我有一个类似的列表:
list(list(goals = c(42L, 52L, 55L),
season = "88",
player = c("a", "b","c")),
list(goals = c(41L,53L, 37L, 40L),
season = "89",
player = c("a","b", "c", "d")))
我想将其转换为长格式的数据框,例如:
goals player season
42 a 88
52 b 88
.
.
41 a 89
53 b 89
.
我可以使用plyr
来实现此目的,例如:
plyr::ldply(mylist, data.frame, .id="season"
我在想可能有使用purrr
或dplyr
的更新方法吗?
答案 0 :(得分:5)
我们可以通过使用list
遍历tibble
将list
元素转换为map
来做到这一点
library(tidyverse)
lst1 %>%
map_df(as_tibble)
# A tibble: 7 x 3
# goals player season
# <dbl> <chr> <dbl>
#1 42 a 88
#2 52 b 88
#3 55 c 88
#4 41 a 89
#5 53 b 89
#6 37 c 89
#7 48 d 89
base R
选项将转换为list
个data.frame
,然后转换为rbind
do.call(rbind, lapply(lst1, as.data.frame))
lst1 <- list(list(goals = c(42, 52, 55), player = c("a", "b", "c"), season = 88),
list(goals = c(41, 53, 37, 48), player = c("a", "b", "c",
"d"), season = 89))
答案 1 :(得分:0)
另一种使用来自R的do.call,rbind和Map以及dplyr的as_tibble做到这一点的方法:
$url = "http://localhost/api/simpel/semua/all";
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);