简体   繁体   English

在列表中查找所有大于指定值的值

[英]Find all values in a list that are greater than a specified value

Using R I'm trying to count the number of elements in a list which are greater than a certain value, and store that number in a variable T . 我试图使用R来计算列表中大于某个值的元素的数量,并将该数量存储在变量T

However, I receive the following error 但是,我收到以下错误

Error: (list) object cannot be coerced to type 'double'". After running the first section of the code I'm looking for the sum of all values > 3 each hist_lst 错误:(列表)对象无法强制键入“ double””。运行代码的第一部分后,我正在寻找所有hist_lst> 3的所有值的总和

when I run the code below 当我运行下面的代码

A <- c(1, 1, 2,3,2,4,5,3,2,1,7)
value <- 5

# incrementations
out_lst <- lapply(A, function(x) x : 5)

hist_lst <- list()
max_len <- max(sapply(out_lst, function(x) length(x)))

for(l in 1:max_len) {
    hist_lst[[l]] <- sapply(out_lst, function(x) x[l])
    hist(hist_lst[[l]])
}

for(l in 1:length(hist_lst)) {
    for(i in 1:length(hist_lst[i])) {
        T[l] = sum(hist_lst[i] > 3)
    }
}

you need to put double brackets in: 您需要在以下位置加上双括号:

 T[l] = sum(hist_lst[[i]] > 3)

but if I understand what you want you can replace the last double loop with 但是如果我了解您想要的内容,则可以将最后一个双循环替换为

T=unlist(lapply(hist_lst,function(x) sum(x>3,na.rm=T)))

> T
[1] 3 4 6 6 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM