可重現的代碼:
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
我更喜歡的行為是,如果成分沒有轉換成長格式,即保持成分作為一個串列,而不是每個成分的一行。有什么辦法可以得到這種行為嗎?
我不想一直使用關系資料庫,想知道是否有一種簡單的方法來處理這個問題。
uj5u.com熱心網友回復:
小題大做
您可以將它們存盤為tibble非常擅長存盤list列的 a,只需修改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
從原始解決方案嵌套
或者,您可以完全保留原始解決方案,tibble然后通過使用nest將其ingredients轉回list.
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
最后,如果你想堅持使用基礎 R,你可以I(list())在@Akrun 的評論中使用like。
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
uj5u.com熱心網友回復:
這是一種快速而骯臟的方法
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"))
基于這個答案
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380636.html
上一篇:按組選擇值出現次數最少的行
