I have a data.frame with many columns (~50). Some of them are character, some are numeric and 3 of them I use for grouping.
I need to:
- remove NAs from numeric columns
- calculate the mean of each of the numeric columns
- extract the first element of the character columns
Let's say, we're using modified iris data as below:
data(iris)
iris$year <- rep(c(2000,3000),each=25) ## for grouping
iris$color <- rep(c("red","green","blue"),each=50) ## character column
iris[1,] <- NA ## introducing NAs
I have ~50 columns in total, numeric and character mixed together. I've been trying something like:
giris <- group_by(iris, Species, year)
cls <- unlist(sapply(giris, class)) ## find out classes
action <- ifelse(cls == "numeric", "mean", "first")
action <- paste(action)
summarise_each(giris, action)
What I get is means for all columns in a group followed by columns with the first values in respective group. And NAs are not handled... Which is not exactly what I seek...
Help anyone?