如何使用dplyr融合和转换数据帧?

Kou*_*ndy 40 r reshape melt dplyr

最近我使用dplyr进行所有数据操作,它是一个很好的工具.但是我无法使用dplyr融合或转换数据框.有没有办法做到这一点?现在我正在使用reshape2来达到这个目的.

我想'dplyr'解决方案:

require(reshape2)
data(iris)
dat <- melt(iris,id.vars="Species")
Run Code Online (Sandbox Code Playgroud)

kon*_*vas 66

接班人reshape2tidyr.的等效melt()dcast()gather()spread()分别.那么等价于您的代码

library(tidyr)
data(iris)
dat <- gather(iris, variable, value, -Species)
Run Code Online (Sandbox Code Playgroud)

如果已magrittr导入,则可以使用管道运算符,例如dplyr,写入

dat <- iris %>% gather(variable, value, -Species)
Run Code Online (Sandbox Code Playgroud)

请注意,您需要明确指定变量和值名称,与in不同melt().我发现语法gather()非常方便,因为您可以只指定要转换为长格式的列,或者通过在前面添加' - '来指定要保留在新数据框中的列(就像上面的Species一样) ),输入比输入快一点melt().但是,我注意到至少在我的机器上,tidyr可能明显慢于reshape2.

编辑在回复下面@hadley的评论时,我发布了一些比较PC上两个功能的时序信息.

library(microbenchmark)
microbenchmark(
    melt = melt(iris,id.vars="Species"), 
    gather = gather(iris, variable, value, -Species)
)
# Unit: microseconds
#    expr     min       lq  median       uq      max neval
#    melt 278.829 290.7420 295.797 320.5730  389.626   100
#  gather 536.974 552.2515 567.395 683.2515 1488.229   100

set.seed(1)
iris1 <- iris[sample(1:nrow(iris), 1e6, replace = T), ] 
system.time(melt(iris1,id.vars="Species"))
#    user  system elapsed 
#   0.012   0.024   0.036 
system.time(gather(iris1, variable, value, -Species))
#    user  system elapsed 
#   0.364   0.024   0.387 

sessionInfo()
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# 
# locale:
#  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
# [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] reshape2_1.4         microbenchmark_1.3-0 magrittr_1.0.1      
# [4] tidyr_0.1           
# 
# loaded via a namespace (and not attached):
# [1] assertthat_0.1 dplyr_0.2      parallel_3.1.1 plyr_1.8.1     Rcpp_0.11.2   
# [6] stringr_0.6.2  tools_3.1.1   
Run Code Online (Sandbox Code Playgroud)

  • 很棒的答案,哈德利的好工作,但只解决了一半的问题!一个传播的例子也会很好 (3认同)
  • 作为此答案的更新,从 tidyr 1.0.0 开始,gather() 和 spread() 现在是ivot_longer() 和ivot_wider(),尽管运行类似的基准测试表明melt 仍然比gather 或pivot_longer 更快(基于tidyR 1.1)。 4)、pivot_longer 目前是三个选项中最慢的。 (3认同)

小智 5

另外,演员阵容也可以使用 tidyr::spread()

你的例子

library(reshape2)
library(tidyr)
library(dplyr)

# example data : `mini_iris`
(mini_iris <- iris[c(1, 51, 101), ])

# melt
(melted1 <- mini_iris %>% melt(id.vars = "Species"))         # on reshape2
(melted2 <- mini_iris %>% gather(variable, value, -Species)) # on tidyr

# cast
melted1 %>% dcast(Species ~ variable, value.var = "value") # on reshape2
melted2 %>% spread(variable, value)                        # on tidyr
Run Code Online (Sandbox Code Playgroud)