重塑R中的数据帧

时间:2009-10-07 18:55:43

标签: r dataframe reshape

我遇到了重塑大型数据帧的困难。我过去一直很幸运地避免重塑问题,这也意味着我很糟糕。

我当前的数据框看起来像这样:

unique_id    seq   response    detailed.name    treatment 
a            N1     123.23     descr. of N1     T1
a            N2     231.12     descr. of N2     T1
a            N3     231.23     descr. of N3     T1
...
b            N1     343.23     descr. of N1     T2
b            N2     281.13     descr. of N2     T2
b            N3     901.23     descr. of N3     T2
...

我想:

seq    detailed.name   T1           T2
N1     descr. of N1    123.23       343.23
N2     descr. of N2    231.12       281.13
N3     descr. of N3    231.23       901.23

我查看了整形包,但我不确定如何将处理因子转换为单独的列名。

谢谢!

编辑:我尝试在我的本地计算机上运行此程序(4GB双核iMac 3.06Ghz)并且它始终失败:

> d.tmp.2 <- cast(d.tmp, `SEQ_ID` + `GENE_INFO` ~ treatments)
Aggregation requires fun.aggregate: length used as default
R(5751) malloc: *** mmap(size=647168) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

当我有机会的时候,我会尝试在我们更大的机器上运行它。

5 个答案:

答案 0 :(得分:18)

重塑对我来说似乎总是很棘手,但似乎总是有点试错。这是我最终找到的结果:

> x
  unique_id seq response detailed.name treatment
1         a  N1   123.23           dN1        T1
2         a  N2   231.12           dN2        T1
3         a  N3   231.23           dN3        T1
4         b  N1   343.23           dN1        T2
5         b  N2   281.13           dN2        T2
6         b  N3   901.23           dN3        T2

> x2 <- melt(x, c("seq", "detailed.name", "treatment"), "response")
> x2
  seq detailed.name treatment variable  value
1  N1           dN1        T1 response 123.23
2  N2           dN2        T1 response 231.12
3  N3           dN3        T1 response 231.23
4  N1           dN1        T2 response 343.23
5  N2           dN2        T2 response 281.13
6  N3           dN3        T2 response 901.23

> cast(x2, seq + detailed.name ~ treatment)
  seq detailed.name     T1     T2
1  N1           dN1 123.23 343.23
2  N2           dN2 231.12 281.13
3  N3           dN3 231.23 901.23

您的原始数据已经是长格式,但不是融合/强制转换使用的长格式。所以我重新融化了。第二个参数(id.vars)是不易融化的事物列表。第三个参数(measure.vars)是变化的事物列表。

然后,演员使用公式。波浪号的左侧是保持不变的东西,波浪号的右侧是用于调节值列的列。

或多或少......!

答案 1 :(得分:6)

基于Harlan的答案 - 如果数据已经是长格式,并且在cast调用中指定了列保持值,则可以避免重熔步骤。

> x <- read.table(textConnection("  unique_id seq response detailed.name treatment
+ 1         a  N1   123.23           dN1        T1
+ 2         a  N2   231.12           dN2        T1
+ 3         a  N3   231.23           dN3        T1
+ 4         b  N1   343.23           dN1        T2
+ 5         b  N2   281.13           dN2        T2
+ 6         b  N3   901.23           dN3        T2"))
> 
> cast(x, seq + detailed.name ~ treatment, value = "response")
  seq detailed.name     T1     T2
1  N1           dN1 123.23 343.23
2  N2           dN2 231.12 281.13
3  N3           dN3 231.23 901.23

答案 2 :(得分:3)

另一种选择是使用spread

中的tidyr
library(tidyr) 
Wide1 <- spread(x[-1], treatment, response)
Wide1
#  seq detailed.name     T1     T2
#1  N1           dN1 123.23 343.23
#2  N2           dN2 231.12 281.13
#3  N3           dN3 231.23 901.23

相反的行动由gather

执行
gather(Wide1, detailed.name, response, T1:T2)
#  seq detailed.name detailed.name response
#1  N1           dN1            T1   123.23
#2  N2           dN2            T1   231.12
#3  N3           dN3            T1   231.23
#4  N1           dN1            T2   343.23
#5  N2           dN2            T2   281.13
#6  N3           dN3            T2   901.23

此外,来自dcast.data.table

data.table
library(data.table)
dcast.data.table(setDT(x), seq + detailed.name~treatment,
                                          value.var='response')
#   seq detailed.name     T1     T2
#1:  N1           dN1 123.23 343.23
#2:  N2           dN2 231.12 281.13
#3:  N3           dN3 231.23 901.23

数据

x <- structure(list(unique_id = structure(c(1L, 1L, 1L, 2L, 2L, 2L
), .Label = c("a", "b"), class = "factor"), seq = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("N1", "N2", "N3"), class = "factor"), 
response = c(123.23, 231.12, 231.23, 343.23, 281.13, 901.23
), detailed.name = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("dN1", 
"dN2", "dN3"), class = "factor"), treatment = structure(c(1L, 
1L, 1L, 2L, 2L, 2L), .Label = c("T1", "T2"), class = "factor")), .Names =
c("unique_id", "seq", "response", "detailed.name", "treatment"), class = 
"data.frame", row.names = c(NA, -6L))

答案 3 :(得分:2)

您还可以使用reshape包中的stats功能。我没有您的样本数据集,但它看起来像这样:

reshape(x, idvar=c("seq","detailed.name"), timevar="treatment", direction="wide")

答案 4 :(得分:1)

如果您希望使用reshape2获得相同的结果,这是reshape包的更快且更高效的内存重写,那么以下内容将起作用。

主要更改是当您希望dcast输出cast时使用data.frame功能。这取代了cast

reshape功能
library(reshape2)

x = read.table(text = "unique_id seq   response  detailed.name treatment
                           a      N1    123.23         dN1        T1
                           a      N2    231.12         dN2        T1
                           a      N3    231.23         dN3        T1
                           b      N1    343.23         dN1        T2
                           b      N2    281.13         dN2        T2
                           b      N3    901.23         dN3        T2", 
sep = "", header = TRUE)

x

y <- dcast(x, seq + detailed.name ~ treatment, value.var = "response")
y
#   seq detailed.name     T1     T2
# 1  N1           dN1 123.23 343.23
# 2  N2           dN2 231.12 281.13
# 3  N3           dN3 231.23 901.23

# EDIT to show how to return to the original data set:

melt(y, id.vars=c('seq', 'detailed.name'), variable.name='T', value.name='response')

#   seq detailed.name  T response
# 1  N1           dN1 T1   123.23
# 2  N2           dN2 T1   231.12
# 3  N3           dN3 T1   231.23
# 4  N1           dN1 T2   343.23
# 5  N2           dN2 T2   281.13
# 6  N3           dN3 T2   901.23