在R中使用长度可变的熔体/铸造 [英] Using melt / cast with variables of uneven length in R

查看:55
本文介绍了在R中使用长度可变的熔体/铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个很大的数据框,希望将其旋转,以使列中的变量成为顶部的行。

I'm working with a large data frame that I want to pivot, so that variables in a column become rows across the top.

我发现在这种情况下,reshape软件包非常有用,除了强制转换功能默认为fun.aggregate = length。大概是因为我是按案例执行这些操作的,而测得的变量数量因案例而异。

I've found the reshape package very useful in such cases, except that the cast function defaults to fun.aggregate=length. Presumably this is because I'm performing these operations by "case" and the number of variables measured varies among cases.

我想进行透视,以便在透视数据框中将丢失的变量表示为 NA。

I would like to pivot so that missing variables are denoted as "NA"s in the pivoted data frame.

因此,换句话说,我想从像这样的复杂数据帧中查找:

So, in other words, I want to go from a molten data frame like this:

Case | Variable | Value
 1         1        2.3
 1         2        2.1
 1         3        1.3
 2         1        4.3
 2         2        2.5
 3         1        1.8
 3         2        1.9
 3         3        2.3
 3         4        2.2

到这样的事情:

Case | Variable 1 | Variable 2 | Variable 3 | Variable 4
 1         2.3          2.1          1.3         NA
 2         4.3          2.5          NA          NA
 3         1.8          1.9          2.3         2.2 

代码dcast(data,...〜Variable)再次默认为fun.aggregate = length,它不保留原始值。

The code dcast(data,...~Variable) again defaults to fun.aggregate=length, which does not preserve the original values.

感谢您的帮助,让我知道是否有任何不清楚的地方!

Thanks for your help, and let me know if anything is unclear!

推荐答案

cast 调用中的变量。 Reshape希望 Value 列被称为 value ,因此它会发出警告,但仍然可以正常工作。它使用 fun.aggregate = length 的原因是因为公式中缺少 Case

It is just a matter of including all of the variables in the cast call. Reshape expects the Value column to be called value, so it throws a warning, but still works fine. The reason that it was using fun.aggregate=length is because of the missing Case in the formula. It was aggregating over the values in Case.

尝试: cast(data,Case 〜Variable)

data <- data.frame(Case=c(1,1,1,2,2,3,3,3,3),
  Variable=c(1,2,3,1,2,1,2,3,4),
  Value=c(2.3,2.1,1.3,4.3,2.5,1.8,1.9,2.3,2.2))

cast(data,Case~Variable)
Using Value as value column.  Use the value argument to cast to override this choice
  Case   1   2   3   4
1    1 2.3 2.1 1.3  NA
2    2 4.3 2.5  NA  NA
3    3 1.8 1.9 2.3 2.2

编辑:作为对@Jon评论的回应。如果数据框中还有一个变量,该怎么办?

as a response to the comment from @Jon. What do you do if there is one more variable in the data frame?

data <- data.frame(expt=c(1,1,1,1,2,2,2,2,2),
               func=c(1,1,1,2,2,3,3,3,3),
               variable=c(1,2,3,1,2,1,2,3,4),
               value=c(2.3,2.1,1.3,4.3,2.5,1.8,1.9,2.3,2.2))

cast(data,expt+variable~func)
  expt variable   1   2   3
1    1        1 2.3 4.3  NA
2    1        2 2.1  NA  NA
3    1        3 1.3  NA  NA
4    2        1  NA  NA 1.8
5    2        2  NA 2.5 1.9
6    2        3  NA  NA 2.3
7    2        4  NA  NA 2.2

这篇关于在R中使用长度可变的熔体/铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆