![](/img/trans.png)
[英]R - save as data.frame all elements of a list of lists efficiently
[英]How can I save a list of lists as a column into a data.frame object in R
可重现的代码:
Meal <- function(name, ingredients, time_to_cook = NA, time_of_day = "Dinner", cuisine = NA) {
m <- list(name = name, ingredients = ingredients, time = time_to_cook, meal = time_of_day, cuisine = cuisine)
m
}
burritos <- Meal("Burrito", ingredients = c("Tortilla", "Chicken Breast", "Sour Cream", "Pepper", "Onion", "Chili Paste"), time_to_cook = "Fast", cuisine = "Mexican")
pizza <- Meal("Pizza", ingredients = c("Flour", "Tinned Tomato", "Mozarella", "Garlic"))
rbind(data.frame(pizza), data.frame(burritos))
name ingredients time meal cuisine
1 Pizza Flour <NA> Dinner <NA>
2 Pizza Tinned Tomato <NA> Dinner <NA>
3 Pizza Mozarella <NA> Dinner <NA>
4 Pizza Garlic <NA> Dinner <NA>
5 Burrito Tortilla Fast Dinner Mexican
6 Burrito Chicken Breast Fast Dinner Mexican
7 Burrito Sour Cream Fast Dinner Mexican
8 Burrito Pepper Fast Dinner Mexican
9 Burrito Onion Fast Dinner Mexican
10 Burrito Chili Paste Fast Dinner Mexican
我更喜欢的行为是,如果成分没有转换为长格式,即将成分保留为列表,而不是每种成分一行。 有什么办法可以让我得到这种行为?
我不想一路 go 并使用关系数据库,想知道是否有一种简单的方法来处理这个问题。
您可以将它们存储为非常擅长存储list
列的tibble
,只需修改Meal()
即可。
library(tibble)
Meal <- function(name, ingredients, time_to_cook = NA, time_of_day = "Dinner", cuisine = NA) {
m <- tibble(name = name, ingredients = list(ingredients), time = time_to_cook, meal = time_of_day, cuisine = cuisine)
m
}
burritos <- Meal("Burrito", ingredients = c("Tortilla", "Chicken Breast", "Sour Cream", "Pepper", "Onion", "Chili Paste"), time_to_cook = "Fast", cuisine = "Mexican")
pizza <- Meal("Pizza", ingredients = c("Flour", "Tinned Tomato", "Mozarella", "Garlic"))
rbind(pizza, burritos)
#> # A tibble: 2 × 5
#> name ingredients time meal cuisine
#> <chr> <list> <chr> <chr> <chr>
#> 1 Pizza <chr [4]> <NA> Dinner <NA>
#> 2 Burrito <chr [6]> Fast Dinner Mexican
或者,您可以完全保留原始解决方案,稍后通过使用nest
将ingredients
转换回list
来转换为tibble
。
library(tidyr)
rbind(data.frame(pizza), data.frame(burritos)) %>%
nest(ingredients = ingredients)
#> # A tibble: 2 × 5
#> name time meal cuisine ingredients
#> <chr> <chr> <chr> <chr> <list>
#> 1 Pizza <NA> Dinner <NA> <tibble [4 × 1]>
#> 2 Burrito Fast Dinner Mexican <tibble [6 × 1]>
最后,如果您想坚持使用基本 R,您可以像 @Akrun 的评论中一样使用I(list())
。
Meal <- function(name, ingredients, time_to_cook = NA, time_of_day = "Dinner", cuisine = NA) {
m <- data.frame(name = name, ingredients = I(list(ingredients)), time = time_to_cook, meal = time_of_day, cuisine = cuisine)
m
}
burritos <- Meal("Burrito", ingredients = c("Tortilla", "Chicken Breast", "Sour Cream", "Pepper", "Onion", "Chili Paste"), time_to_cook = "Fast", cuisine = "Mexican")
pizza <- Meal("Pizza", ingredients = c("Flour", "Tinned Tomato", "Mozarella", "Garlic"))
rbind(pizza, burritos)
#> name ingredients time meal cuisine
#> 1 Pizza Flour, T.... <NA> Dinner <NA>
#> 2 Burrito Tortilla.... Fast Dinner Mexican
这是一种快速而肮脏的方法
Meal <- function(name, ingredients, time_to_cook = NA, time_of_day = "Dinner", cuisine = NA) {
m <- data.frame(name = name, time = time_to_cook, meal = time_of_day, cuisine = cuisine)
m$ingredients <- list(ingredients)
m
}
burritos <- Meal("Burrito", ingredients = c("Tortilla", "Chicken Breast", "Sour Cream", "Pepper", "Onion", "Chili Paste"), time_to_cook = "Fast", cuisine = "Mexican")
pizza <- Meal("Pizza", ingredients = c("Flour", "Tinned Tomato", "Mozarella", "Garlic"))
基于这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.