Hi Steven,

One would be

with(yourdataset, aggregate(x, list(lc1, id), mean))
Group.1 Group.2        x
1       85     ga1 45.47261
2       95     ga1 53.38831
3      105     ga1 58.18282
4      115     ga1 63.77469
5      125     ga1 66.98222
6       85     ga2 47.55711
7       95     ga2 54.78450
8      105     ga2 60.67968
9      115     ga2 64.83192
10     125     ga2 66.98222
11      85     ga3 51.99211
12      95     ga3 56.70202
13     105     ga3 61.69574
14     115     ga3 65.90707
15     125     ga3 66.98222

HTH,
Jorge


On Fri, Mar 25, 2011 at 1:36 PM, Steven Ranney <> 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@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to