[R] averaging rows based on string¿?

Dennis Murphy djmuser at gmail.com
Sat Jul 23 17:46:44 CEST 2011


Hi:

Try this:

labs <- c("abcdef","abcgg","tgthefdk","tgtijuel","tgtnjmoi","gbnt","dlift")
dat <- c(0.5,0.25,1,2,16,0.250,4)
dframe <- data.frame(labs, dat, stringsAsFactors = FALSE)
dframe$lab2 <- factor(substr(dframe$lab, 1, 3))
aggregate(dat ~ lab2, data = dframe, FUN = mean)
  lab2      dat
1  abc 0.375000
2  dli 4.000000
3  gbn 0.250000
4  tgt 6.333333

HTH,
Dennis


On Fri, Jul 22, 2011 at 1:00 PM, alfredo <alfredotello at gmail.com> wrote:
> Hi Folks,
>
> Ran into something I'd really like to do in R simply/elegantly, but my R -
> coding skills seem surpassed. This is the thing. Imagine the following data:
>
> labs<-c("abcdef","abcgg","tgthefdk","tgtijuel","tgtnjmoi","gbnt","dlift")
> dat<-c(0.5,0.25,1,2,16,0.250,4)
> dframe<-data.frame(labs,dat)
>
> I would like to average the values in "dat" according to specific
> string/text in the name of their row names described by "labs". For example,
> average the values in "dat" if the first three (or more) letters in their
> corresponding "labs" are the same. This would give the following vector:
>
> 0.375000 6.333333 0.250000 4.000000 # in which 0.375 is the average of
> "abcdef" and "abcgg", etc.
>
> I hope I've made myself (kind'a) clear and apologise otherwise. Any ideas?
>
> Thanks for your help!
>
> A
>
> --
> View this message in context: http://r.789695.n4.nabble.com/averaging-rows-based-on-string-tp3687689p3687689.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list