作者:hh呢喃_845 | 来源:互联网 | 2024-11-20 18:13
我目前处理的是一个包含1000个唯一ID及其相关数值的数据框。我的目标是根据每个ID将特定的两列合并成一行。虽然尝试过使用reshape包中的cast函数,但它仅能将单一列转换为行。下面是一个具体的数据示例:
DF <- data.frame(ID=c("x1","x1","x1","x1","x2","x2","x2","x2"),
name1=c("T1","T1","T2","T2","T1","T1","T2","T2"),
name2=c("C1","C2","C1","C2","C1","C2","C1","C2"),
T1_val=c(1.1,1.1,2.3,2.3,1.8,1.8,7.9,7.9),
C1_val=c(1.1,2.6,1.1,2.6,1.8,3.6,1.8,3.6))
> DF
ID name1 name2 T1_val C1_val
1 x1 T1 C1 1.1 1.1
2 x1 T1 C2 1.1 2.6
3 x1 T2 C1 2.3 1.1
4 x1 T2 C2 2.3 2.6
5 x2 T1 C1 1.8 1.8
6 x2 T1 C2 1.8 3.6
7 x2 T2 C1 7.9 1.8
8 x2 T2 C2 7.9 3.6
期望得到的结果如下所示:
ID T1 T2 C1 C2
x1 1.1 2.3 1.1 2.6
x2 1.8 7.9 1.8 3.6
感谢您的帮助!
解决方案
通过结合使用dplyr和tidyr这两个强大的数据操作包,可以非常简便地达到上述目的。以下是具体的代码实现:
library(dplyr)
library(tidyr)
DF %>%
pivot_wider(names_from = c(name1, name2), values_from = c(T1_val, C1_val))
运行以上代码后,将会得到如下结果:
# A tibble: 2 × 5
ID T1_T1 C1_C1 T2_T2 C2_C2
1 x1 1.1 1.1 2.3 2.6
2 x2 1.8 1.8 7.9 3.6
如果您希望深入学习这两个包的更多功能,建议查阅官方文档或相关的快速参考指南。