为什么mutate不接受data.frame作为要嵌套的列?

时间:2019-03-27 16:28:27

标签: r dplyr purrr mutate

library(tidyverse)
a = data.frame(c1 = c(1,2,3), c2 = c("a","b","c"))
b = data.frame(c3 = c(TRUE,FALSE,TRUE))
a %>% mutate(c_nested = b)

产生错误:

  

错误:列c_nested的类数据不受支持。框架

如何添加包含嵌套data.frame的列?

非常感谢!

2 个答案:

答案 0 :(得分:0)

我们可以将其作为list列传递

a %>% 
   mutate(c_nested = list(b))

答案 1 :(得分:0)

res <- 
  a %>% 
  `$<-`(c_nested, b)

str(res)

# 'data.frame': 3 obs. of  3 variables:
#  $ c1      : num  1 2 3
#  $ c2      : Factor w/ 3 levels "a","b","c": 1 2 3
#  $ c_nested:'data.frame': 3 obs. of  1 variable:
#   ..$ c3: logi  TRUE FALSE TRUE