本文介绍了将嵌套列表(不等长度)转换为数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个嵌套的列表;对于某些索引,一些变量缺失。 [[1]] sk ques pval 10sfsf0.05 [[2]] sk ques pval diff 24wwww0.110.3 [[3]] sk ques pval diff imp 24wwww0.110.32 如何将其转换为数据帧,第一行数据$ diff [1] = NA? 以上情况将是具有5个变量和3个观察值的数据帧。 数据帧中的变量数将是列表元素中唯一名称的数量, 和列表中缺少的值将被替换为NA。 谢谢, 编辑:数据格式 list(structure(c(10,sfsf,0.05),.Names = c (sk,ques,pval)),结构(c(24,wwww,0.11,0.3),.Names = c(sk,ques,pval,diff)),结构(c(24,wwww,0.11,0.3,2), (sk,ques,pval,diff,imp))) 解决方案我们得到列表元素('indx')的长度通过循环使用 sapply 。在最新版本的 R 中,我们可以使用长度替换 sapply(.. ,长度) step。我们将每个元素的长度从'indx'(长度< $ ; - ),从而在列表结尾处填充 NA 小于 max 长度。我们可以 rbind 列表元素,转换为 data.frame 并更改列名。 indx< - sapply(lst,length) #indx< length(lst) res max(indx)))) colnames(res)< - names(lst [[which.max(indx)]]) res #sk ques pval diff imp #1 10 sfsf 0.05< NA> < NA> #2 24 wwww 0.11 0.3 #3 24 wwww 0.11 0.3 2 数据 lst pval)),结构(c(24,wwww,0.11,0.3),.Names = c(sk,ques pval,diff)),结构(c(24,wwww,0.11,0.3,2),.Names = c(sk,ques ,pval,diff,imp))) I have a nested list; for some indices, some variables are missing.[[1]] sk ques pval "10" "sfsf" "0.05" [[2]] sk ques pval diff "24" "wwww" "0.11" "0.3" [[3]] sk ques pval diff imp "24" "wwww" "0.11" "0.3" "2" How can I convert this to data frame, where for the first row, data$diff[1] = NA?Above case will be data frame with 5 variables and 3 observations.The number of variables in the data frame will be number of unique names in list elements,and missing values inside the list will be replaced with NA's.Thank you,EDIT : Data formatlist(structure(c("10", "sfsf", "0.05"), .Names = c("sk", "ques", "pval")), structure(c("24", "wwww", "0.11", "0.3"), .Names = c("sk", "ques", "pval", "diff")), structure(c("24", "wwww", "0.11", "0.3", "2"), .Names = c("sk", "ques", "pval", "diff", "imp"))) 解决方案 We get the length of list element ('indx') by looping with sapply. In the recent version of R, we can use lengths to replace the sapply(.., length) step. We change the length of each element to the max length from the 'indx' (length<-) and thereby pad NA values at the end of the list elements with length less than the max length. We can rbind the list elements, convert to data.frame and change the column names. indx <- sapply(lst, length) #indx <- lengths(lst) res <- as.data.frame(do.call(rbind,lapply(lst, `length<-`, max(indx)))) colnames(res) <- names(lst[[which.max(indx)]]) res # sk ques pval diff imp #1 10 sfsf 0.05 <NA> <NA> #2 24 wwww 0.11 0.3 <NA> #3 24 wwww 0.11 0.3 2data lst <- list(structure(c("10", "sfsf", "0.05"), .Names = c("sk", "ques", "pval")), structure(c("24", "wwww", "0.11", "0.3"), .Names = c("sk", "ques", "pval", "diff")), structure(c("24", "wwww", "0.11", "0.3", "2"), .Names = c("sk", "ques", "pval", "diff", "imp"))) 这篇关于将嵌套列表(不等长度)转换为数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 13:41