取消列出数据框中的列并列出 [英] Unlist column in data frame with listed
问题描述
我有一个包含多个级别的列表,我希望将数据级别放入一个数据帧中,其中变量chr折叠为单个字符串.
I have a list with multiple levels that I would like to the data level into a data frame, where the variable chr is collapsed into single strings.
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")))
我想将其转换为如下所示的数据框:
I would like to transform this into a data frame that looks like this:
reach chr nr company
1 2 A, B, C 3 Company A
2 2 A, B, C 3 Company B
使用dplyr和data.table,我已经走了这么远.
Using dplyr and data.table I've come this far.
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()"的字符串.
However, chr column contains strings with "list()" wrapped around it.
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中的列表折叠为字符串或因子?
A) Is there a better way to unlist this kind of list and turn it into a data frame?
B) How do I collapse the lists in chr to strings or factors?
推荐答案
使用 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
或者,在将列表转换为数据表之前,可以操作 chr
.
rbindlist(lapply(myList$data, function(x) {
x$chr = toString(x$chr)
return(as.data.table(x))
}))
reach chr nr company
1: 2 A, B, C 3 Company A
2: 2 A, B, C 3 Company B
这篇关于取消列出数据框中的列并列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!