[R] Function on columns of a dataframe
LogLord
nils.schoof at web.de
Fri Jul 9 15:37:29 CEST 2010
Hi,
I would like to assign the largest value of a column to a specific category
and repeat this for each column (v1 - v4).
> x=c(1:12)
> cat=c("cat1","cat5","cat2","cat2","cat1","cat5","cat3","cat4","cat5","cat2","cat3","cat6")
> v1=rnorm(12,0.5,0.1)
> v2=rnorm(12,0.3,0.2)
> v3=rnorm(12,0.4,0.1)
> v4=rnorm(12,0.6,0.3)
> bla=data.frame(x,cat,v1,v2,v3,v4)
> bla
x cat v1 v2 v3 v4
1 1 cat1 0.4013144 0.54839317 0.3946393 0.8679266
2 2 cat5 0.4595873 0.45788906 0.4030078 0.5919596
3 3 cat2 0.4542865 0.21516928 0.2777649 0.6112099
4 4 cat2 0.4787950 0.06252512 0.5095611 0.6450795
5 5 cat1 0.4910746 0.56591049 0.5151813 0.8465181
6 6 cat5 0.4194397 0.16592579 0.4361643 0.6415192
7 7 cat3 0.6148564 0.32240342 0.2690108 0.7114133
8 8 cat4 0.6174652 0.28076152 0.4577064 -0.2567284
9 9 cat5 0.4775395 0.28611768 0.4660210 0.4634120
10 10 cat2 0.4802962 0.03715569 0.4506361 1.0063235
11 11 cat3 0.6495094 0.33303172 0.3352933 1.4390324
12 12 cat6 0.4891481 0.45355589 0.3880739 0.7831656
>
I can assign this by the sqldf() command for each column but I would like to
automate this as I have many columns.
> select=sqldf("select cat, max(v1) FROM bla GROUP BY cat")
> select
cat max(v1)
1 cat1 0.4910746
2 cat2 0.4802962
3 cat3 0.6495094
4 cat4 0.6174652
5 cat5 0.4775395
6 cat6 0.4891481
>
Finally, I would like to have a dataframe where where the cat is followed by
each column maximum.
Thanks for your help!
--
View this message in context: http://r.789695.n4.nabble.com/Function-on-columns-of-a-dataframe-tp2283217p2283217.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list