我已经看到了关于如何将数据从水平翻转到垂直的一些很好的解释。但是,在将列更改为行之后,我需要一些不同的组织。
这是我的数据:
Compound Frequency Type
C1 -0.853272929 A
C2 -0.780699577 A
C3 -0.614477598 A
C4 -0.130738078 A
C5 0.142499411 A
C6 0.148570388 A
C7 0.108864569 A
C8 1.036668305 A
C9 0.057404313 A
C10 -0.024941378 A
C1 0.003007008 B
C2 0.104085077 B
C3 0.051143686 B
C4 -1.060236186 B
C5 -0.484587066 B
C6 -0.15934591 B
C7 0.189355481 B
C8 -0.037796111 B
C9 0.522518879 B
C10 -0.184031867 B
C1 0.148570388 C
C2 0.108864569 C
C3 1.036668305 C
C4 0.057404313 C
C5 -0.024941378 C
C6 0.003007008 C
C7 0.104085077 C
C8 0.051143686 C
C9 -1.060236186 C
C10 -0.484587066 C
C1 -0.011113991 D
C2 4.196813014 D
C3 0.133978213 D
C4 -0.252406687 D
C5 0.517614284 D
C6 0.087048873 D
C7 3.316242168 D
C8 4.331790602 D
C9 -0.464324736 D
C10 0.107550844 D
我想转换成这个版本:
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
A -0.853272929 -0.780699577 -0.614477598 -0.130738078 0.142499411 0.148570388 0.108864569 1.036668305 0.057404313 -0.024941378
B 0.003007008 0.104085077 0.051143686 -1.060236186 -0.484587066 -0.15934591 0.189355481 -0.037796111 0.522518879 -0.184031867
C 0.148570388 0.108864569 1.036668305 0.057404313 -0.024941378 0.003007008 0.104085077 0.051143686 -1.060236186 -0.484587066
D -0.011113991 4.196813014 0.133978213 -0.252406687 0.517614284 0.087048873 3.316242168 4.331790602 -0.464324736 0.107550844
如你所见,标题部分消失了。我想在每一行中保留一种类型和新的标题化合物。这是10种化合物的简单版本。我有400个并希望进行这种类型的数据转换。
如果你能帮助我,我会很高兴的。非常感谢你。
最诚挚的问候,
Bahti
答案 0 :(得分:2)
使用tidyr
包
library(tidyr)
data_wide <- spread(data, Compound, Frequency)
data_wide
Type C1 C10 C2 C3 C4 C5 C6 C7 C8
1 A -0.853272929 -0.02494138 -0.7806996 -0.61447760 -0.13073808 0.14249941 0.148570388 0.1088646 1.03666831
2 B 0.003007008 -0.18403187 0.1040851 0.05114369 -1.06023619 -0.48458707 -0.159345910 0.1893555 -0.03779611
3 C 0.148570388 -0.48458707 0.1088646 1.03666831 0.05740431 -0.02494138 0.003007008 0.1040851 0.05114369
4 D -0.011113991 0.10755084 4.1968130 0.13397821 -0.25240669 0.51761428 0.087048873 3.3162422 4.33179060
C9
1 0.05740431
2 0.52251888
3 -1.06023619
4 -0.46432474