简体   繁体   中英

R + reshape : variance of columns of a data.frame

I'm using reshape in R to compute aggregate statistics over columns of a data.frame. Here's my data.frame:

> df
  a a b b ID
1 1 1 1 1  1
2 2 3 2 3  2
3 3 5 3 5  3

which is just a little test data.frame to try and understand the reshape package. I melt, and then cast, to try and find the mean of the a s and the b s:

> melt(df, id = "ID") -> df.m
> cast(df.m, ID ~ variable, fun = mean)
  ID a b
1  1 1 1
2  2 2 2
3  3 3 3

Argh! What? Was hoping the mean of c(2,3) was 2.5 and so on. What's going on? Here's a thing:

> df.m
   ID variable value
1   1        a     1
2   2        a     2
3   3        a     3
4   1        a     1
5   2        a     2
6   3        a     3
7   1        b     1
8   2        b     2
9   3        b     3
10  1        b     1
11  2        b     2
12  3        b     3

what's going on? Where did both my 5 s go? Do I have a very basic misunderstanding going on here? If so: what is it?

这不是有效的数据框,因为列没有唯一的名称。

I updated my answer here to fix this: R: aggregate columns of a data.frame

Apparently, if your data frame doesn't have unique column names, they won't melt properly.

Edit: Instead of having column names of aaabb , apparently you need to have unique column names for melt() to work properly. Minimally a.1 a.2 a.3 b.1 b.2 , or something. After using melt() , your options to get sensible levels for variable is either to use gsub() on the levels of variable to eliminate the disambiguating values, or to use colsplit() to create two new columns. For the dummy names I just gave, that would look like:

levels(df.m$variable) <- gsub("\\..*", "", levels(df.m$variable))
#or
df.m <- cbind(df.m, colsplit(df.m$variable, split = "\\.", names = c("Measure","N")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM