2017-12-03 235 views
1

我想創建一個嵌套的數據框,類似於在tidyr包中嵌套。但我還沒有我的數據在data.frame,所以我不能簡單df %>% group_by(thing) %>% nest()我有3個列表,對應於我試圖把三個列彙集在一起​​。看下面的例子:從單獨的列表創建一個嵌套的數據框

library(tidyverse) 

student = c('bob', 'jane', 'sam') 
avg_score = c(95, 99, 80) 
details = lapply(1:3, 
      function(x) data.frame(answer = sample(c(TRUE, FALSE),10, TRUE), 
            question_num = 1:10)) 

現在,這是我想要做什麼:

my_data_frame = data.frame(student = student, 
          avg_score = avg_score, 
          details = details) 

但是,當然,這不會工作作爲details不是原子公司的名單,而是data.frame列表秒。我也試過:

my_data_frame = data.frame(student = student, 
          avg_score = avg_score, 
          details = nest(details)) 
+0

你可以存儲在'data.frame'使用'I'即'細節= I(信息)一個'list'列)' – akrun

回答

2

對於那些遇到類似問題的人。關鍵是要使用的tibble代替data.frame

library(tidyverse) 

student = c('bob', 'jane', 'sam') 
avg_score = c(95, 99, 80) 
details = lapply(1:3, function(x) data.frame(answer = sample(c(TRUE, FALSE), 10, TRUE), question_num = 1:10)) 

my_nested_data_frame = tibble(student = student, avg_score = avg_score, details = details)