[英]For loop warning in R: number of items to replace is not a multiple of replacement length
[英]R warning: “number of items to replace is not a multiple of replacement length”
寻找关于为什么我得到下面的简单代码警告“要更换的物品数量不是更换长度的倍数”的一些见识(我需要它做什么)?
A_DF <- data.frame(A = c(1,1,2,2,3,4), B = c(0,0,0,0,0,0))
A_DF <- A_DF %>% mutate(B = replace(B, A<=2, A), B = replace(B, A>2, 7))
Warning message:
In x[list] <- values :
number of items to replace is not a multiple of replacement length
输出:
A_DF
A B
1 1 1
2 1 1
3 2 2
4 2 2
5 3 7
6 4 7
问题出在
B = replace(B, A<=2, A)
A的长度为6,将被压缩为B的子集(当A <= 2时)。
您可以使用:
A_DF <- A_DF %>% mutate(B = A, B = replace(B, A>2, 7))
要么:
A_DF$B <- ifelse(A_DF$A>2,7,A_DF$A)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.