簡體   English   中英

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

[英]Why does mutate not accept a data.frame as a column to nest?

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

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

非常感謝!

我們可以將它作為list列傳遞

a %>% 
   mutate(c_nested = list(b))
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM