averaging rows based on string¿?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

averaging rows based on string¿?

alfredo
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
Reply | Threaded
Open this post in threaded view
|

Re: averaging rows based on string¿?

djmuseR
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: averaging rows based on string¿?

alfredo
Hi Dennis,

That works brilliantly! Thanks a lot!

All the best,

A