[英]How can I design an r function that selects specific elements from a list of lists, and returns a dataframe as an output
I'm trying to set up an r function that will select relevant elements from a list, and end up with a dataframe as output. 我正在尝试建立一个r函数,该函数将从列表中选择相关元素,并最终以数据帧作为输出。
Here is the list I'm using: 这是我正在使用的列表:
test_list<-list(set1=list(2, NA, NA, 8, NA, NA, 2), set2=list(4, 6, NA, NA, 2, 1, 1), set3=c(2, 3, 2, 1, NA, NA, NA))
For each element of my list, I'd like to keep only the sublists that contains less than 4 NA elements. 对于列表中的每个元素,我只想保留包含少于4个NA元素的子列表。
Here is the function I've built: 这是我构建的功能:
is.useful <-function(x){ #x is a list of sublists
#I want to keep only the sublists with less than 4 NA elements
vector <-c()
for(i in x){
if(sum(is.na(x[[i]])) <= 3){
vector <-c(vector, unlist(x[[i]]))
}
}
return (vector)
}
Runing is.useful(test_list)
, I'm getting the Error in x[[i]] : type 'list' 正在运行
is.useful(test_list)
,我在x [[i]]中遇到错误:输入“ list”
I don't understand the issue here because: sum(is.na(test_list[["set1"]]))
returns the right answer 4 and unlist(test_list[["set1"]])
also gives me the sublist as a vector ( is.vector(unlist(test_list[["set1"]]))
returns TRUE ) 我在这里不了解这个问题,因为:
sum(is.na(test_list[["set1"]]))
返回正确答案4, unlist(test_list[["set1"]])
也给我该子列表vector( is.vector(unlist(test_list[["set1"]]))
返回TRUE)
I've also tried something else, namely transforming the list of lists into a dataframe, using following command: 我还尝试了其他方法,即使用以下命令将列表列表转换为数据框:
dd <- as.data.frame(matrix(unlist(test_list), nrow=length(unlist(test_list[1]))))
From there, I try to run a very similar function, without the unlist: 从那里,我尝试运行一个非常相似的函数,但不取消列表:
is.useful2 <-function(x){ #x is dataframe
#I want to keep only the vectors with less than 4 NA elements
vector <-c()
for(i in x){
if(sum(is.na(x[i])) <= 3){
vector <-c(vector, (x[i]))
}
}
return (vector)
}
is.useful2(dd)
returns Error in [.data.frame
(x, i) : undefined columns selected is.useful2(dd)
在[.data.frame
(x,i is.useful2(dd)
返回错误:选择了未定义的列
What do I expect? 我期望什么? In this specific example, I expect a dataframe of 2 vectors, set2 and set3, for which I have less than 4 NA values.
在此特定示例中,我希望有2个向量set2和set3的数据帧,其中我的NA值小于4。
I'm a bit lost. 我有点迷路了。 What did I do wrong ?
我做错了什么 ?
Thanks a lot for your help. 非常感谢你的帮助。
hope this helps: 希望这可以帮助:
do.call(cbind,test_list[sapply(test_list,function(x)sum(is.na(unlist(x))))<4])
set2 set3
[1,] 4 2
[2,] 6 3
[3,] NA 2
[4,] NA 1
[5,] 2 NA
[6,] 1 NA
[7,] 1 NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.