Aggregate Command in R, Remove Columns? -


my aggregate command takes mean of of columns based on specific variable. apply function columns. have code in following format aggregate:

aggregate(dataframe,list(name=variable),mean) 

this gives me mean different columns based on specific variable.i selectively choose columns.

if want apply function select columns 2 , 3 (numeric index)

 aggregate(dat1[,2:3], list(name=dat1[,'variable']), fun=mean)  #   name   v1   v2  #1    10.2 11.2  #2    b 12.8  7.6 

or replace numeric index column names

 aggregate(dat1[,c("v1", "v2")], list(name=dat1[,'variable']), fun=mean)  #  name   v1   v2  #1    10.2 11.2  #2    b 12.8  7.6 

or using dplyr

 library(dplyr)  dat1 %>%       group_by(variable) %>%       summarise_each(funs(mean=mean(., na.rm=true)), v1,v2)  #  variable   v1   v2  #1        10.2 11.2  #2        b 12.8  7.6 

or using data.table

 library(data.table)  setdt(dat1)[, lapply(.sd, mean), by=variable, .sdcols=colnames(dat1)[2:3]]  #  variable   v1   v2  #1:        10.2 11.2  #2:        b 12.8  7.6 

data

set.seed(24) dat1 <- cbind(variable=rep(letters[1:2], each=5),        as.data.frame(matrix(sample(1:20, 10*5, replace=true), ncol=5))) 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -