r - How do I make a column that sums only numeric columns? -


i have dataframe lot of columns.

label    col1  col2  col3 meat     10    20    30 veggies  20    30    40 

how make column named sumcol adds col1, col2, col3, , other numeric columns add?

example of sumcol above columns:

sumcol 60 90 

you can use function, takes advantage of select_if , scoped argument is_numeric

myfun <- function(df) {                require(dplyr)                y <- select_if(df, is_numeric)                rowsums(y, na.rm=t)          } 

solution

df$sumcol <- myfun(df) 

output

    label col1 col2 col3 sumcol 1    meat   10   20   30     60 2 veggies   20   30   40     90 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -