Dave M
Dave M

Reputation: 71

How to Split-Apply-Combine for several variables / columns in R

I'd like to perform a function on several variables, by group.

Fake data;

df<-data.frame(rnorm(100,mean=10), 
               rnorm(100,mean=15),
               rnorm(100,mean=20),
               rep(letters[1:10],each=10)
               )
colnames(df)<-c("var1","var2","var3","group1")

In this particular case, I'd like to mean-center each variable by group. I want to return a dataframe with the original and centered variables.

Normally I use PLYR package for this;

library(plyr)    
ddply(df, "group1", transform, centered_var1= scale(var1, scale=FALSE))

However, I haven't been able to successfully loop this function, or think of another minimal-code way to do this.

I'm open to non-PLYR solutions...My main criteria is keeping code to a minimum.

Upvotes: 1

Views: 265

Answers (3)

Josh W.
Josh W.

Reputation: 1123

The colwise function may be what you're looking for.

library("plyr")
ddply(df, .(group1), colwise(scale, scale = FALSE))

Upvotes: 3

Rorschach
Rorschach

Reputation: 32426

Using dplyr

library(dplyr)
df %>% group_by(group1) %>%
  mutate_each(funs(scale(., scale=F))) -> res

Upvotes: 3

Heisenberg
Heisenberg

Reputation: 8806

Is this what you want?

ddply(df, "group1", transform, centered_var1= scale(var1, scale=FALSE),
      centered_var2 = scale(var2, scale=FALSE),
      centered_var3 = scale(var3, scale=FALSE))

Upvotes: 1

Related Questions