[英]cosinor2 package R error column 'Y' must be a 1d atomic vector or a list
[英]R: dplyr “must be a 1d atomic vector or a list” error when using filter_()
我有一個data.frame
中的R稱為possibleVotes
它看起來像這樣(參見后用於代碼生成此數據幀,或CSV表的末尾這里 ):
+------+------+------+------+------+-------------+--------+------+
| Var1 | Var2 | Var3 | Var4 | Var5 | nSuccesses1 | p1 | use1 |
+------+------+------+------+------+-------------+--------+------+
| 0 | 0 | 0 | 1 | 0 | 0 | 0.0225 | 0 |
+------+------+------+------+------+-------------+--------+------+
| 1 | 0 | 0 | 1 | 0 | 1 | 0.1275 | 0 |
+------+------+------+------+------+-------------+--------+------+
| 0 | 1 | 0 | 1 | 0 | 1 | 0.1275 | 0 |
+------+------+------+------+------+-------------+--------+------+
| 1 | 1 | 0 | 1 | 0 | 2 | 0.7225 | 1 |
+------+------+------+------+------+-------------+--------+------+
我正在嘗試使用dplyr
通過如下代碼dplyr
進行過濾:
round <- 1
roundTotal <- 2
pRound1 <- possibleVotes %>%
select_(num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
distinct_() %>%
filter_(paste0("nSuccesses", round) == roundTotal & paste0("use", round) == 1) %>%
select_(paste0("p", round)) %>%
sum()
我的預期結果只是pRound1
為0.7225。 不幸的是我得到這個錯誤:
錯誤:列
p1
必須是一維原子向量或列表
我經歷了每一步,發現:
distinct_()
沒有錯誤。 filter_()
步驟中。 num_range()
不起作用(未選擇列Var1
和Var2
) 我不知道錯誤是什么意思,也不知道為什么num_range()
也不給我想要的結果...
我該如何解決? 謝謝!
編輯:從@Axeman的慷慨幫助后,我嘗試了filter_()
:
filter_(lazyeval::interp(~a == b, a = as.name(paste0("nSuccesses", round)), b = roundTotal)) %>%
filter_(lazyeval::interp(~a == b, a = as.name(paste0("use", round)), b = 1))
不幸的是,它仍然給出:
錯誤:列
p1
必須是一維原子向量或列表
編輯2:我正在使用R 3.4.0運行dplyr 0.5.0。
編輯3:這是我用來生成原始的possibleVotes
數據幀的完整代碼:
possibleVotes <- expand.grid(replicate(5, c(0,1), simplify = FALSE))
pSpecies <- 0.85
round <- 1
roundFor <- 2
roundAgainst <- 0
roundTotal <- roundFor + roundAgainst
possibleVotes[paste0("nSuccesses", round)] <- rowSums(possibleVotes[1:roundTotal])
possibleVotes[paste0("p", round)] <- pSpecies^(possibleVotes[paste0("nSuccesses", round)]) * (1-pSpecies)^(round1Total - possibleVotes[paste0("nSuccesses", round)])
possibleVotes[paste0("use", round)] <- apply(possibleVotes, 1, function(x) if (sum(x[1:roundTotal]) == roundFor) 1 else 0)
一個簡單的技巧是將所有事物paste0
到一起以創建條件,從而免除了原本麻煩的條件構建過程。 您可以建立這樣的條件: paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1")
它會為您提供類似"nSuccesses1==2&use1== 1"
,然后filter_
。
編輯:我所有的代碼:
dmatrix <- t(matrix(c(0,0,0,1,0,0,0.0225,0,
1,0,0,1,0,1,0.1275,0,
0,1,0,1,0,1,0.1275,0,
1,1,0,1,0,2,0.7225,1), nrow = 8, ncol = 4))
possibleVotes <- data.frame(dmatrix)
colnames(possibleVotes) <- c("Var1","Var2","Var3","Var4","Var5","nSuccesses1","p1","use1")
round <- 1
roundTotal <- 2
pRound1 <- possibleVotes %>%
select_(~num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
distinct_() %>%
filter_(paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1")) %>%
select_(paste0("p", round)) %>%
sum()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.