Reshape2 Data Manipulation Data fusion (melt)

Source: Internet
Author: User
Tags vars

The previous article about the cast, must have seen the strong reshape2, of course, in the use of cast with melt this powerful rubbing data ability to perform incisively and vividly.

Let's take a look at the melt function and its characteristics.

Melt (data, ..., na.rm = FALSE, Value.name = "value")

From here, the parameters of the function are relatively simple, and data represents the na.rm to be processed, representing the missing value handling, value.name the name of the column used to rename the value.

In addition, the difficulty of the melt function is that different data structures, the parameters used may not be the same.

First, the data to be fused is an array, a table, and a matrix, so the melt expression is:

Melt (data, varnames = names (dimnames data), ..., na.rm = False, As.is = false, Value.name = "value")

Varnames user-named variable name

Second, the data to be fused is the data frame, then the melt expression is:

Melt (data, Id.vars, measure.vars, variable.name = "variable", ..., na.rm = FALSE, Value.name = "value", factorsasstrings = TRUE)

Id.vars sets the variables that are displayed separately after blending, can be represented by the variable position and name, and no write indicates that all non-measure.vars values are used

Measure.vars usually varies according to the Id.vars settings

Finally, the data to be fused is a list, then the melt expression is:

Melt (data, ..., level = 1)

Here's a look at some concrete examples.

data<-Array (c (1:22, NA, "Wo"), C (2,3,4)) data

Melt (data)

It can be seen that the data fusion, readability than the case of the array is much stronger, var1 represents the array of rows, var2 represents the array of columns, VAR3 represents the array sequence.

For example, the 18 position is the 3rd array, 2 rows 3 columns, 11 is the 2nd array, and 1 rows 3 columns.

Melt (A, na.rm = TRUE)

You can see that the missing values in the array are removed.

Melt (data, varnames=c ("Hang", "lie", "Zu"))

Rename each variable after merging.

Let's look at the case where the data is a data frame.

X<-data.frame (Id=1:6,              name=c ("Wang", "Zhang", "Li", "Chen", "Zhao", "song"),              shuxue=c (89,85,68,79,96,53) ,              yuwen=c (77,68,86,87,92,63)) x

Melt (X,id=c ("id", "name"))

 

Melt (x,id=1:2,variable.name= "Kemu", value.name= "Zhi")

Melt (X,measure.vars=c ("id", "name"))

Finally, take a look at the case where the data is a list

shuju<-list (Matrix (1:4, ncol=2), Array (1:27, C (3,3,3)))
Shuju

The organization of this list is more complicated, it's a little difficult to read.

Below melt the result of fusion

Melt (Shuju)

You can see that the data becomes very concise.

Reshape2 Data Manipulation Data fusion (melt)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.