[英]how to find elements from a list that are not present in another list in r
[英]R: Find all elements from a list and write findings to another list
我基本上有兩個列表 A 和 B(列表列表),我想找到 B 中的所有元素,它們與 A 中的多個條件匹配。我做了:
for(k in 1:length(A)) {
matches[k] <- length(which(condition 1 &
condition 2 &
... &
condition n)
if(matches[k] == 0) {
matches[k] <- -9999
} else {
matches[k] <- which(condition 1 &
condition 2 &
... &
condition n)
}
}
但這只能保存一個發現。 我收到警告:
“要替換的項目數不是替換長度的倍數”
我明白問題是什么。 which() 在 B 中找到多個元素,但不能將多個元素寫入列表的一行。 關於如何解決這個問題的任何想法?
干杯
您的matches
似乎是一個向量。 嘗試使用列表,這樣您就可以存儲單個或多個元素。
我將通過以下方式修改您的代碼:
matches <- list()
for(k in 1:length(A)) {
matches[[k]] <- length(which(condition 1 &
condition 2 &
... &
condition n)
if(matches[[k]] == 0) {
matches[[k]] <- -9999
} else {
matches[[k]] <- which(condition 1 &
condition 2 &
... &
condition n)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.