我将一堆docx文件加载到列表中,然后尝试将结果列表列表转换为数据框。
下面的可重现示例不读取docx文件,仅演示了我编写的lapply代码
# Creating a list to somewhat
# replicate the list of lists
# I get from lapply read_docx
a <- c("1","0","art","3","4")
x <- cbind(as.data.frame(a),as.data.frame(a),as.data.frame(a))
b <- c("a", "b", "c")
names(x) = b
y <- as.list(x)
shingled <- as.data.frame("a")
names(shingled) = "vaaar"
# putting together each list in
# y into a single data frame
# Note: even though as.data.frame(y[1])
# gives, surprise surprise, a data frame,
# the code below returns a list
all <- lapply(y, function(x){
exer <- as.data.frame(x)
names(exer) = "vaaar"
shingled <- rbind(shingled,
exer)}
)
此处提供相同的代码: https://pastebin.com/rw24jaMN
答案 0 :(得分:2)
对我来说,将每个元素转换为数据帧然后将它们组合在一起会更容易。
all_dat <- do.call(rbind, lapply(y, as.data.frame))
names(all_dat) <- "vaaar"
all_dat
# vaaar
# a.1 1
# a.2 0
# a.3 art
# a.4 3
# a.5 4
# b.1 1
# b.2 0
# b.3 art
# b.4 3
# b.5 4
# c.1 1
# c.2 0
# c.3 art
# c.4 3
# c.5 4
对我来说,尚不清楚您想要的输出是什么,但是do.call(rbind, ...)
也适用于您创建的列表。
all_dat <- do.call(rbind, all)
all_dat
# vaaar
# a.1 a
# a.2 1
# a.3 0
# a.4 art
# a.5 3
# a.6 4
# b.1 a
# b.2 1
# b.3 0
# b.4 art
# b.5 3
# b.6 4
# c.1 a
# c.2 1
# c.3 0
# c.4 art
# c.5 3
# c.6 4
答案 1 :(得分:2)
如果有人想避免命名变量名(例如a.1, a.2...b.1,b.2 etc
)并想使用dplyr
链,则解决方案可能是:
library(tidyverse)
bind_rows(y) %>% gather(Key, "vaaar") %>% as.data.frame()
# Key vaaar
# 1 a 1
# 2 a 0
# 3 a art
# 4 a 3
# 5 a 4
# 6 b 1
# 7 b 0
# 8 b art
# 9 b 3
# 10 b 4
# 11 c 1
# 12 c 0
# 13 c art
# 14 c 3
# 15 c 4
答案 2 :(得分:0)
找到一个简单的答案:
as.data.frame(unlist(y))
优雅地替换了整个lapply并发挥了作用