mutate和rowSums排除列 [英] mutate and rowSums exclude columns
问题描述
类似于: mutate rowSums排除一列,但在我的情况下,我真的想要使用选择
删除一列或一组列
我试图理解为什么这样的本性,不行。
d< - data.frame(
Alpha =字母[1:26],
Beta = rnorm(26),
Epsilon = rnorm(26),
Gamma = rnorm(26)
)
我以为这可以工作,但是给我一个奇怪的错误:
Total = Beta + Gamma
d< - mutate(d,Total = rowSums(select(d,-Epsilon,-Alpha)))
错误:所有select()输入必须解析为整数列位置。
以下不要:
* -structure(1:26,.Label = c(a,b,c,d,e,f g,h,i ...
另外:警告信息:
在Ops.factor(1:26)中:' - '对于因素
无效code>
我希望能够在长链中做到这一点,并保持dplyr风格...它打击我很奇怪,这是非常困难的,因为没有使用典型的dplyr语法,这是非常简单的:
d $ Total< - rowSums (select(d,-Alpha,-Epsilon))#这样可以!
@akrun 关于这个问题,关于 dplyr
解决方案,我实际上会使用 do
:
d%>%
do({
。$ Total< - rowSums(select(。,-Epsilon ,-Alpha))
。
})
Similar to: mutate rowSums exclude one column but in my case, I really want to be able to use select
to remove a specific column or set of columns
I'm trying to understand why something of this nature, won't work.
d <- data.frame(
Alpha = letters[1:26],
Beta = rnorm(26),
Epsilon = rnorm(26),
Gamma = rnorm(26)
)
I thought this would work, but it's giving me a strange error:
# Total = Beta + Gamma
d <- mutate(d,Total = rowSums(select(d,-Epsilon,-Alpha)))
Error: All select() inputs must resolve to integer column positions.
The following do not:
* -structure(1:26, .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i...
In addition: Warning message:
In Ops.factor(1:26) : ‘-’ not meaningful for factors
I'd like to be able to do this in a long chain, and keep it "dplyr style"... it strikes me as odd that this is so difficult given that it's really straightforward without using typical dplyr syntax:
d$Total <- rowSums(select(d, -Alpha, -Epsilon)) # This works!
@akrun provided already a relevant link about this problem. As about dplyr
solution, I would actually use do
:
d %>%
do({
.$Total <- rowSums(select(., -Epsilon, -Alpha))
.
})
这篇关于mutate和rowSums排除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!