Reputation: 3663
I have a dataframe with a lot of columns.
LABEL COL1 COL2 COL3
Meat 10 20 30
Veggies 20 30 40
How do I make column named SUMCOL
that adds up COL1
, COL2
, COL3
, and any other numeric columns I add?
Example of SUMCOL
with just the above columns:
SUMCOL
60
90
Upvotes: 3
Views: 7303
Reputation: 2546
I know this is an old post, but there's a tidy way to do this with just dplyr:
library(dplyr)
#Create dataset
data <- tibble(LABEL = c("Meat", "Veggies"),
COL1 = c(10, 20),
COL2 = c(20, 30),
COL3 = c(30, 40))
data %>%
mutate(SUMCOL = select(., starts_with("COL")) %>%
rowSums(na.rm = TRUE))
In case anyone is unfamiliar with this syntax, it basically says "make (mutate) a new column called SUMCOL. To do so, select all columns (that's the period), but perform rowSums only on the columns that start with "COL" (as an aside, you also could list out the columns with c("COL1", "COL2", "COL3") and ignore any missing values.
Upvotes: -1
Reputation: 3663
I ended up using this code:
df$SUMCOL <- rowSums(df[sapply(df, is.numeric)], na.rm = TRUE)
Upvotes: 3
Reputation: 13591
You can use this function, which takes advantage of select_if
and scoped argument is_numeric
myfun <- function(df) {
require(dplyr)
y <- select_if(df, is_numeric)
rowSums(y, na.rm=T)
}
df$SUMCOL <- myfun(df)
LABEL COL1 COL2 COL3 SUMCOL
1 Meat 10 20 30 60
2 Veggies 20 30 40 90
Upvotes: 0