[R] averaging X of specific Y (latitude)

David L Carlson dcarlson at tamu.edu
Tue Dec 18 04:07:23 CET 2012


You should change your email format to text (you keep sending messages in
html format).

Where are the butterfly names? Are they not in your original data? Create
your data.frame from the original data (which presumably has the butterfly
names in it already). Then use dput() if you need to email the data to
r-help. You cannot compute statistics or graphics from the result of the
dput() function.

Yes you can produce a barplot. Type ?barplot to get the instructions.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


From: Elaine Kuo [mailto:elaine.kuo.tw at gmail.com] 
Sent: Monday, December 17, 2012 6:37 PM
To: dcarlson at tamu.edu
Cc: r-help at r-project.org
Subject: Re: [R] averaging X of specific Y (latitude)

Thank you, David.

Now I know how to use dput. 

Two more questions conjured up when running the code:
1. If there are about 500 species of butterflies, 
    please kindly advise if it is possible to input the butterfly names (for
.Label) using code, 
    instead of keying in them one by one.

2. Aggregrate command
    Last time we produced the mean of range sizes.
    Please kindly advise if it is possible to produce barplot showing the
value range, instead of mean.

Thank you
Elaine

code
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
>>
On Tue, Dec 18, 2012 at 12:27 AM, David L Carlson <dcarlson at tamu.edu> wrote:
You are only using dput to create a version of your data that you can post
in your email along with your question. You do not use it in your analysis.
You originally posted a table of your data that requires extra steps for us
to convert into a useable form so I suggested you use dput in the future to
create a version of the data that is easy for r-help subscribers to use in
order to recreate your data. That makes it easier to answer your question. 
 
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
 
From: Elaine Kuo [mailto:elaine.kuo.tw at gmail.com] 
Sent: Monday, December 17, 2012 5:56 AM
To: dcarlson at tamu.edu; r-help at r-project.org

Subject: Re: [R] averaging X of specific Y (latitude)
 
Another question (I am sure it is the last one tentatively):
 
After creating a text.file using dput (let's name the created file
"datam"), 
please kindly advise how to use the text file in writing the code using
"structure" like below.
 
Thank you.
 
code
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)))
 
Elaine
 
 
On Mon, Dec 17, 2012 at 7:40 PM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
One more question:
 
Please kindly explain 
why it is written as "row.names = c(NA, -11L))."
(in the code below)
 
I am unsure why it is "negative" 11.
Thank you .
 
Elaine
 
code
 
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)))
 

>>
>>
>> ----- Original Message -----
>> From: David L Carlson <dcarlson at tamu.edu>
>> To: 'Elaine Kuo' <elaine.kuo.tw at gmail.com>; r-help at r-project.org;
>> 'r-sig-geo' <r-sig-geo at stat.math.ethz.ch>
>> Cc:
>> Sent: Sunday, December 16, 2012 11:32 AM
>> Subject: Re: [R] averaging X of specific Y (latitude)
>>
>> 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.
>>
>> ______________________________________________
>> 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.
>>
>>
>>
>
>       [[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