我有一个通过API通过JSON创建的数据框。
df <- structure(list(date = c("2020-09-10", "2020-09-09", "2020-09-08",
"2020-09-07", "2020-09-06"), name = c("England", "England", "England",
"England", "England"), code = c("E92000001", "E92000001", "E92000001",
"E92000001", "E92000001"), cases = structure(list(daily = c(2578L,
2286L, 2094L, 2528L, 2576L), cumulative = c(309133L, 306555L,
304269L, 302175L, 299647L)), class = "data.frame", row.names = c(NA,
-5L)), deaths = structure(list(daily = c(0L, 6L, 8L, 11L, 9L),
cumulative = c(36944L, 36944L, 36938L, 36930L, 36919L)), class = "data.frame", row.names = c(NA,
-5L))), class = "data.frame", row.names = c(NA, -5L))
如果您使用以下数据查看数据:head(df)
您得到了:
date name code cases.daily cases.cumulative deaths.daily deaths.cumulative
1 2020-09-10 England E92000001 2578 309133 0 36944
2 2020-09-09 England E92000001 2286 306555 6 36944
3 2020-09-08 England E92000001 2094 304269 8 36938
4 2020-09-07 England E92000001 2528 302175 11 36930
5 2020-09-06 England E92000001 2576 299647 9 36919
但是
colnames(df)
给出:
[1] "date" "name" "code" "cases" "deaths"
我知道那里有2个嵌套的DF,但是我不知道如何使用类似的东西:
unnest(cols = c(cases, deaths))
...在未嵌套的DF中获得7列,其列名和结构如上面的头例所示。我希望能够在tidyverse
管道中执行此操作,以避免创建中间对象。
答案 0 :(得分:2)
这应该有效:
do.call(cbind, df)