[R] "for" loop assistance -

Henrique Dallazuanna wwwhsd at gmail.com
Fri Mar 25 20:13:00 CET 2011


Try this:

aggregate(x ~ lc1 + id, DF, FUN = mean)

Where DF is your data.frame

On Fri, Mar 25, 2011 at 2:36 PM, Steven Ranney <steven.ranney at gmail.com> wrote:
> All –
>
> I have an example data frame
>
> x       lc1     id
> 43.38812035     85      ga1
> 47.55710661     85      ga1
> 47.55710661     85      ga2
> 47.55710661     85      ga2
> 51.99211429     85      ga3
> 51.99211429     85      ga3
> 51.99211429     95      ga1
> 54.78449958     95      ga1
> 54.78449958     95      ga2
> 54.78449958     95      ga2
> 56.70201864     95      ga3
> 56.70201864     95      ga3
> 56.70201864     105     ga1
> 59.66361903     105     ga1
> 59.66361903     105     ga2
> 61.69573564     105     ga2
> 61.69573564     105     ga3
> 61.69573564     105     ga3
> 63.77469479     115     ga1
> 63.77469479     115     ga1
> 64.83191994     115     ga2
> 64.83191994     115     ga2
> 64.83191994     115     ga3
> 66.98222118     115     ga3
> 66.98222118     125     ga1
> 66.98222118     125     ga1
> 66.98222118     125     ga2
> 66.98222118     125     ga2
> 66.98222118     125     ga3
> 66.98222118     125     ga3
>
> and I’m trying to extract means for every lc1 and id level so that I
> would have a data frame that looks like
>
> x.m     lc1     id
> x.1     085     ga1
> x.2     085     ga2
> x.3     085     ga3
> x.4     095     ga1
> x.5     095     ga2
> x.6     095     ga3
> x.7     105     ga1
> x.8     105     ga2
> x.9     105     ga3
> x.10    115     ga1
> x.11    115     ga2
> x.12    115     ga3
> x.13    125     ga1
> x.14    125     ga2
> x.15    125     ga3
>
> Now, I can use brute force to get the second data frame by
>
> write.table(tapply(x[lc1=="085"], id[lc1=="085"], mean), "file.xls", sep=",")
> write.table(tapply(x[lc1=="095"], id[lc1=="095"], mean), “file.xls",
> sep=",", append=T)
> write.table(tapply(x[lc1=="105"], id[lc1=="105"], mean), “file.xls",
> sep=",", append=T)
> etc…
>
> and add the values for the lc1 column in the .xls file until I’ve
> worked my way through every level for lc1 then read the file back into
> R, but this would require a great deal of my time.  (I have 72 levels
> for lc1 and 346 levels for id totalling over 20,000 lines.)
>
> I am confident that there is a simple, more elegant solution available
> to me that I am overlooking.  I am sure that I could use a “for” loop,
> but as someone that is new to R programming, I am unsure of how to go
> about creating the for loop to build the second data frame.  I've
> tried to modify existing for loops that I already have but have been
> unsuccessful.
>
> Do you have any suggestions?
>
> Thank you –
>
> Steven Ranney
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list