使用以下功能,我搜索列表(my_list)并返回列表中具有“ .Positivedata”的任何列的元素。
#Function to give me the names of elements in a list with any column with character
".Positivedata" (Works OK)
names(my_list)[sapply(my_list, function(x) any(grep(".Positivedata", names(x))))]
现在,我想修改该函数,以返回“ .Positivedata”列上哪个名称的值为“ 4”
names(my_list)[sapply(my_list, function(x) any(grep(".Positivedata", names(x))) )]
答案 0 :(得分:0)
我们遍历list
,用grep
子集化该列,检查其是否等于4,然后使用which
获取位置
lapply(my_list, function(x) which(x[, grep("\\.Positivedata", names(x))] == 4))
如果有多个列,那么获取行/列索引会更好
lapply(my_list, function(x)
which(x[, grep("\\.Positivedata", names(x))] == 4, arr.ind = TRUE))
如果我们要在.Positivedata
列中具有值'4'的列名
sapply(my_list, function(x) {
nm1 <- grep("\\.Positivedata", names(x), value = TRUE)
nm1[sapply(x[nm1], function(x) any(x==4))]
})