我想知道在我下面的代码中,是否有一个等价的reshape2::dcast
从tidyverse
甚至Base R生成下面相同的输出?
library(reshape2)
dat=structure(list(row = c("L2DA", "L2DA", "L2DA", "L2DA", "L2DA",
"L2DA", "L2DF", "L2DF", "L2DF", "L2DF", "L2DF", "L2G", "L2G",
"L2G", "L2M", "L2P", "L2P", "L2R", "L2R", "L2R", "L2R", "L2R",
"L2R", "L2R", "L2V", "L2V", "L2V", "L2V", "L2DF", "L2G", "L2L",
"L2M", "L2P", "L2V", "L2G", "L2L", "L2M", "L2P", "L2V", "L2L",
"L2M", "L2P", "L2L", "L2L", "L2M", "L2DA", "L2DF", "L2G", "L2L",
"L2M", "L2P", "L2V", "L2G", "L2L", "L2M", "L2P"),
col = c("L2DF",
"L2G", "L2L", "L2M", "L2P", "L2V", "L2G", "L2L", "L2M", "L2P",
"L2V", "L2L", "L2M", "L2P", "L2L", "L2L", "L2M", "L2DA", "L2DF",
"L2G", "L2L", "L2M", "L2P", "L2V", "L2G", "L2L", "L2M", "L2P",
"L2DA", "L2DA", "L2DA", "L2DA", "L2DA", "L2DA", "L2DF", "L2DF",
"L2DF", "L2DF", "L2DF", "L2G", "L2G", "L2G", "L2M", "L2P", "L2P",
"L2R", "L2R", "L2R", "L2R", "L2R", "L2R", "L2R", "L2V", "L2V",
"L2V", "L2V"),
R = c(0.64, 0.55, 0.42, 0.56, 0.57, 0.48, 0.37,
0.24, 0.29, 0.48, 0.35, 0.48, 0.57, 0.29, 0.51, 0.33, 0.48, 0.58,
0.46, 0.52, 0.56, 0.49, 0.47, 0.51, 0.53, 0.52, 0.53, 0.42, 0.64,
0.55, 0.42, 0.56, 0.57, 0.48, 0.37, 0.24, 0.29, 0.48, 0.35, 0.48,
0.57, 0.29, 0.51, 0.33, 0.48, 0.58, 0.46, 0.52, 0.56, 0.49, 0.47,
0.51, 0.53, 0.52, 0.53, 0.42)), row.names = c(NA, -56L), class = "data.frame")
reshape2::dcast(dat, row ~ col, value.var = "R") ## HERE
### OUTPUT:
row L2DA L2DF L2G L2L L2M L2P L2R L2V
1 L2DA NA 0.64 0.55 0.42 0.56 0.57 0.58 0.48
2 L2DF 0.64 NA 0.37 0.24 0.29 0.48 0.46 0.35
3 L2G 0.55 0.37 NA 0.48 0.57 0.29 0.52 0.53
4 L2L 0.42 0.24 0.48 NA 0.51 0.33 0.56 0.52
5 L2M 0.56 0.29 0.57 0.51 NA 0.48 0.49 0.53
6 L2P 0.57 0.48 0.29 0.33 0.48 NA 0.47 0.42
7 L2R 0.58 0.46 0.52 0.56 0.49 0.47 NA 0.51
8 L2V 0.48 0.35 0.53 0.52 0.53 0.42 0.51 NA
1条答案
按热度按时间e5nszbig1#
在基R中,我们可以使用
reshape()
,setdiff()
和order()
:tidyverse
: