How can I use sapply on a dataframe within dplyr? I thought I'd just reference the current object at a given line with a . symbol.
#mtcars but with first row equal to mtcars' rownames
mtcars2 <-
cbind(data.frame("names"=rownames(mtcars)), mtcars)
#If I want to make columns 2-the end of mtcars numeric, this works:
sapply(mtcars[,2:ncol(mtcars)], as.numeric)
#Does not work in dplyr
mtcars2 %>%
sapply(.[,2:ncol(.)], as.numeric)
in dplyr the error is :
Error in match.fun(FUN) :
'.[, 2:ncol(.)]' is not a function, character or symbol
Thanks - but doesn't this lose column 1 in the process? I just want to do the sapply to all columns of mtcars2 except column1 (or use a mutate_all like you have), but i still need the first column. De-selecting the column in this way loses it right?