[R] averaging X of specific Y (latitude)
David L Carlson
dcarlson at tamu.edu
Sun Dec 16 17:32:58 CET 2012
It is better to use dput() in R to create a text version of your data for us
to work with. The aggregate command below gives you the mean ranges by
butterfly species and latititude and saves the result as Bfly. The
colnames() command simply renames the columns:
> dta <- structure(list(Species = structure(1:11, .Label = c("Butterfly A1",
+ "Butterfly A2", "Butterfly A3", "Butterfly A4", "Butterfly B1",
+ "Butterfly B2", "Butterfly B3", "Butterfly B4", "Butterfly B5",
+ "Butterfly C1", "Butterfly C2"), class = "factor"), Range = c(130.5,
+ 450.68, 1102.38, 893.34, 820.2, 872.2, 488.2, 620.11, 982.78,
+ 720.32, 912.2), Latitude = c(9.45, 10.2, 9.3, 16.4, 10.54, 10.87,
+ 16.79, 18.3, 12.98, 12.67, 18.07)), .Names = c("Species", "Range",
+ "Latitude"), class = "data.frame", row.names = c(NA, -11L))
> Bfly <- aggregate(Range~Species+floor(Latitude), dta, mean)
> colnames(Bfly) <- c("Species", "Latitude", "Mean")
> Bfly
Species Latitude Mean
1 Butterfly A1 9 130.50
2 Butterfly A3 9 1102.38
3 Butterfly A2 10 450.68
4 Butterfly B1 10 820.20
5 Butterfly B2 10 872.20
6 Butterfly B5 12 982.78
7 Butterfly C1 12 720.32
8 Butterfly A4 16 893.34
9 Butterfly B3 16 488.20
10 Butterfly B4 18 620.11
11 Butterfly C2 18 912.20
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Elaine Kuo
> Sent: Saturday, December 15, 2012 10:15 PM
> To: r-help at r-project.org; r-sig-geo
> Subject: [R] averaging X of specific Y (latitude)
>
> Hello
>
>
>
> I have a table describing butterfly range traits.
>
> It is composed of three columns as below
>
>
>
> Species name range size (X) latitude of range midpoint (Y)
>
>
>
> There are 11 kinds of butterflies.
>
> Each has its range size, and the latitude of each range midpoint ranges
> from 9 to 19.
>
> I would like to have the average range size of every degree of
> latitude.
>
> For example, the average range size of latitude degree 10 (10.0-10.99:
> Butterfly A2, B1, B2)
>
> Please kindly help with R code to calculate the average values.
>
> Thank you.
>
>
> Elaine
>
>
> The details are as followed.
>
>
>
> Butterfly A1 130.5 9.45
>
> Butterfly A2 450.68 10.2
>
> Butterfly A3 1102.38 9.3
>
> Butterfly A4 893.34 16.4
>
> Butterfly B1 820.2 10.54
>
> Butterfly B2 872.2 10.87
>
> Butterfly B3 488.2 16.79
>
> Butterfly B4 620.11 18.3
>
> Butterfly B5 982.78 12.98
>
> Butterfly C1 720.32 12.67
>
> Butterfly C2 912.2 18.07
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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