使用tidyverse或R base替换reshape2::decast()

wwtsj6pe  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(90)

我想知道在我下面的代码中,是否有一个等价的reshape2::dcasttidyverse甚至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
e5nszbig

e5nszbig1#

在基R中,我们可以使用reshape()setdiff()order()

dat1 <- reshape(dat, idvar = "row", timevar = "col", direction = "wide")
dat1 <- dat1[, c("row", sort(setdiff(names(dat1), "row")))]
dat1[order(dat1$row), ]

   row R.L2DA R.L2DF R.L2G R.L2L R.L2M R.L2P R.L2R R.L2V
1  L2DA     NA   0.64  0.55  0.42  0.56  0.57  0.58  0.48
7  L2DF   0.64     NA  0.37  0.24  0.29  0.48  0.46  0.35
12  L2G   0.55   0.37    NA  0.48  0.57  0.29  0.52  0.53
31  L2L   0.42   0.24  0.48    NA  0.51  0.33  0.56  0.52
15  L2M   0.56   0.29  0.57  0.51    NA  0.48  0.49  0.53
16  L2P   0.57   0.48  0.29  0.33  0.48    NA  0.47  0.42
18  L2R   0.58   0.46  0.52  0.56  0.49  0.47    NA  0.51
25  L2V   0.48   0.35  0.53  0.52  0.53  0.42  0.51    NA

tidyverse

library(dplyr)
library(tidyr)

dat %>%
  pivot_wider(names_from = col, values_from = R) %>%
  arrange(row) %>%
  select(row, sort(setdiff(names(.), "row")))

相关问题