我有一个包含多个级别的列表,我希望将数据级别转换为数据框,其中变量chr折叠为单个字符串。
myList <- list(total_reach = list(4),
data = list(list(reach = 2,
chr = list("A", "B", "C"),
nr = 3,
company = "Company A"),
list(reach = 2,
chr = list("A", "B", "C"),
nr = 3,
company = "Company B")))
我想将其转换为如下所示的数据框:
reach chr nr company
1 2 A, B, C 3 Company A
2 2 A, B, C 3 Company B
使用dplyr和data.table我到目前为止。
library(data.table)
library(dplyr)
df <- data.frame(rbindlist(myList[2])) %>% t() %>% as.data.frame()
colnames(df) <- names(myList$data[[1]])
rownames(df) <- c(1:nrow(df))
df$chr <- as.character(df$chr)
df <- df %>%
mutate_all(funs(unlist(.recursive = F, use.names = F)))
但是,chr列包含带有“list()”的字符串。
reach chr nr company
1 2 list("A", "B", "C") 3 Company A
2 2 list("A", "B", "C") 3 Company B
A)有没有更好的方法来取消这种列表并将其转换为数据框?
B)如何将chr中的列表折叠为字符串或因子?
答案 0 :(得分:4)
以下是使用tidyverse
library(tidyverse)
myList[-1] %>%
map_df(transpose) %>%
mutate_at(vars(c('reach', 'nr', 'company')), funs(unlist))
答案 1 :(得分:3)
使用data.table
,您可以尝试
library(data.table)
rbindlist(lapply(myList$data, as.data.table))[, .(chr = toString(chr)),
by = .(reach, nr, company)]
reach nr company chr 1: 2 3 Company A A, B, C 2: 2 3 Company B A, B, C
请注意,使用as.data.table
或as.data.frame
:
rbindlist(lapply(myList$data, as.data.table))
reach chr nr company 1: 2 A 3 Company A 2: 2 B 3 Company A 3: 2 C 3 Company A 4: 2 A 3 Company B 5: 2 B 3 Company B 6: 2 C 3 Company B
rbindlist(lapply(myList$data, as.data.frame))
reach chr..A. chr..B. chr..C. nr company 1: 2 A B C 3 Company A 2: 2 A B C 3 Company B
或者,{<1}}可以在将列表转换为data.table之前操作:
chr
rbindlist(lapply(myList$data, function(x) { x$chr = toString(x$chr) return(as.data.table(x)) }))
答案 2 :(得分:1)
我使用rbind
将所有内容放在一起,然后使用chr
重新格式化sapply
列
library(magrittr)
myList$data %>%
do.call(rbind,.) %>%
transform(chr %<>% sapply(paste,collapse=","))
# reach chr nr company
# 1 2 A,B,C 3 Company A
# 2 2 A,B,C 3 Company B
几个月后编辑:
一行更长但更惯用tidyverse
变体:
library(tidyverse)
myList$data %>%
map_df(as_tibble) %>%
group_by(reach,nr,company) %>%
summarize_at("chr",paste,collapse=",")