r - getting mean of variables based on names -
i have vector hu <-rnorm(20)
names names(hu) <- c(1:5,1:5,6:10,3:7)
how group them can take means based on names?
try
tapply(hu, names(hu), fun=mean)
if need in order 1:10, convert names(hu) 'character' 'numeric'
tapply(hu, as.numeric(names(hu)), fun=mean)
or
unique(ave(hu, names(hu)))
Comments
Post a Comment