我有一个具有以下结构的列表列表:
a = list("t" = list('abc'), "c" = list('def','ghi'))
b = list("t" = list('jk','kl'), "c" = list('lmn'))
c = list("t" = list('op','pq','z'), "c" = list('qrs','tuv','wxy'))
ls = list('one'=a,'two'=b,'three'=c)
我需要将其存储在文本文件中。由于并非所有元素的长度都相同,因此在将其转换为数据框并写入文件之前,我需要填充nan。我想要一个看起来像这样的文件(此处大致显示为.csv,但.txt可以):
t1, t2, t3, c1, c2, c3
one,abc,nan,nan,def,ghi,nan
two,jk,kl,nan,lmn,nan,nan
three,op,pq,z,qrs,tuv,wxy
我是R的新手,所以我对如何执行此操作只有一般的认识,无法弄清语法。伪代码中是这样的:
lapply(ls, fill nans)
lapply(ls, unlist)
lapply(ls, names=[t1,t2,t3,c1,c2,c3])
df=data.frame(ls)
write.table(df)
有人可以引导我完成这个过程吗?
编辑:我能够在以下方面取得一些进展
:ellength <- function(ls,i) {return(length(ls[[i]]))}
fillna <- function(ls,i,m) {
if (length(ls[[i]])<m) {
return(append(ls[[i]],vector('list',length=m-length(ls[[i]]))))
}
else {return(ls[[i]])}
}
make_col <- function(ls,i){
return(lapply(ls,fillna,i=i,m=max(unlist(lapply(ls,ellength,i=i)))))
}
> matrix(list(make_col(ls,'t'),make_col(ls,'c')))
[,1]
[1,] List,3
[2,] List,3
但是我仍然无法以任何连贯的方式将其写入文件。这在Python中是如此简单。我肯定错过了什么。帮助吗?
答案 0 :(得分:2)
这完全有帮助吗?
library(tidyr)
library(dplyr)
a = list("t" = 'abc', "c" = list('def','ghi'))
b = list("t" = 'jk', "c" = list('lmn'))
c = list("t" = 'op', "c" = list('qrs','tuv','wxy'))
ls = list('one'=a,'two'=b,'three'=c)
#unlist and turn into a df
lx <- as.data.frame( unlist(ls),stringsAsFactors = FALSE)
#make rownames as column
lx$nms <- rownames(lx)
#split nms column so you can transpose your data
lx <- separate(lx, nms, c("v1","v2"), sep = "[.]")
lx <- mutate(lx, v3 = `unlist(ls)`) %>%
select(-`unlist(ls)`)
#transpose your data - it fills with NA
#NaN is a numeric field so you can't use it to fill character variables
lx2 <- spread(lx,v2,v3)
如果您希望对变量进行折叠,可以使用ifelse
替换某些列中的NA
:
lx2 <- mutate(lx2, c_1 = ifelse(is.na(c),c1,c))
lx2 <- mutate(lx2, t_1 = ifelse(is.na(t),t1,t))
lx3 <- lx2[c('v1','c_1','c2','c3','t_1','t2','t3')]
答案 1 :(得分:1)
考虑在不列出嵌套列表之后构建数据帧列表:
char_vec <- unlist(ls)
df_list <- lapply(names(ls), function(x) {
tmp <- data.frame(t(char_vec[names(char_vec)[grep(x, names(char_vec))]]),
stringsAsFactors = FALSE)
names(tmp) <- gsub(".*\\.", "", names(tmp))
return(tmp)
})
df_list
# [[1]]
# t c1 c2
# 1 abc def ghi
# [[2]]
# t1 t2 c
# 1 jk kl lmn
# [[3]]
# t1 t2 t3 c1 c2 c3
# 1 op pq z qrs tuv wxy
要将所有数据框项目绑定在一起,可以使用dplyr
,data.table
甚至base
R:
dplyr (带有bind_rows
)
final_df1 <- bind_rows(df_list)
# CLEAN UP AND RE-ORDER COLUMNS
final_df1 <- transform(final_df1,
t1 = ifelse(is.na(t), t1, t),
t = NULL,
c1 = ifelse(is.na(c), c1, c),
c = NULL
)
final_df1 <- final_df1[order(names(final_df1))]
data.table (带有rbindlist
)
final_df2 <- data.frame(rbindlist(df_list, fill=TRUE))
# CLEAN UP AND RE-ORDER COLUMNS
final_df2 <- transform(final_df2,
t1 = ifelse(is.na(t), t1, t),
t = NULL,
c1 = ifelse(is.na(c), c1, c),
c = NULL
)
final_df2 <- final_df2[order(names(final_df2))]
基本 R (带有do.call
)
# RETRIEVE ALL COLUMN NAMES
nms <- names(unlist(df_list))
df_list <- lapply(df_list, function(df){
# CREATE BLANK COLUMNS FOR rbind
for(i in nms) {
if(!i %in% names(df)) {
df[[i]] <- NA
}
}
# CLEAN UP AND RE-ORDER COLUMNS
df <- within(df, {t1 <- ifelse(is.na(t), t1, t)
t <- NULL
c1 <- ifelse(is.na(c), c1, c)
c <- NULL
})
return(df[order(names(df))])
})
final_df3 <- do.call(rbind, df_list)
输出
final_df1
# c1 c2 c3 t1 t2 t3
# 1 def ghi <NA> abc <NA> <NA>
# 2 lmn <NA> <NA> jk kl <NA>
# 3 qrs tuv wxy op pq z
identical(final_df1, final_df2)
# [1] TRUE
identical(final_df1, final_df3)
# [1] TRUE
identical(final_df2, final_df3)
# [1] TRUE