I have a dataframe (or a tibble no matter) with many columns and I want to apply a function (let's say rowSums) on only 7 of them, but I don't want to get reed of the others. The trick is that I want to do so in a pipe sequence - create (or read the data) - apply the function - optional operation after that
Here is a reproductible exemple on a dataframe where I would like to rowSums on the first 3 columns
data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10))
the way I would usually do it is
data$sum <- rowSums(data[,1:3])
but I want something like this
data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10)) %>%
mutate(sum = rowSums())
Thanks for your help !