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 usedo
:I'm only just learning dplyr, so perhaps it is because of version upgrades, but this does now work:
These days, I usually see folks use the dot notation:
A slightly more manageable example:
NOTE:
The question you linked to has an updated answer that shows yet another new method that demonstrates some new dplyr features: