[R] a more elegant way to get percentages?
Dimitris Rizopoulos
dimitris.rizopoulos at med.kuleuven.be
Thu Mar 13 16:02:55 CET 2008
well, check what the apply() gives for each row of 'x', i.e.,
x <- read.table(textConnection("locat val
1 a 5
2 b 5
3 b 15
4 c 5
5 c 20
6 c 5
7 c 10
8 d 5
9 d 15
10 d 10"), header = TRUE)
# apply() uses as.matrix() for data frames
# 1st row of 'x'
y <- as.matrix(x)[1, ]
x$val[x$locat == y]
# 2nd row of 'x'
y <- as.matrix(x)[2, ]
x$val[x$locat == y]
# 3rd row of 'x'
y <- as.matrix(x)[3, ]
x$val[x$locat == y]
# 4th row of 'x'
y <- as.matrix(x)[4, ]
x$val[x$locat == y]
in which 'y' is auto-replicated to match the length of 'x$locat' each
time.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Ted Harding" <Ted.Harding at manchester.ac.uk>
To: <r-help at r-project.org>
Sent: Thursday, March 13, 2008 3:15 PM
Subject: Re: [R] a more elegant way to get percentages?
> Now that people have answered Monica's query, can someone help me?!!
> See below.
>
> On 13-Mar-08 13:36:03, Monica Pisica wrote:
>>
>> Hi,
>>
>> I am trying to get percentages in a more elegant way. I have a
>> data.frame with locations and values (counts) of species at that
>> location. Each location is repeated for each species i have values
>> for
>> and i would like to get percentages of each species at that
>> location. I
>> am not sure if i am clear in my explanations so i will paste my
>> code
>> below:
>>
>>#####################
>>
>>> x
>> locat val
>> 1 a 5
>> 2 b 5
>> 3 b 15
>> 4 c 5
>> 5 c 20
>> 6 c 5
>> 7 c 10
>> 8 d 5
>> 9 d 15
>> 10 d 10
>
> With Monica's dataframe as above, the answer would be 100*x[,1]/z
> where we want z to be c(5,20,20,40,40,40,40,30,30,30).
>
> So, intending to give Monica a helpful answer, I tried
>
>> apply(x,1,function(y) sum(x[x[,1]==y,2]))
> 1 2 3 4 5 6 7 8 9 10
> 5 15 15 30 30 30 30 15 15 15
>
> and similarly
>
>> apply(x,1,function(y) sum(x$val[x$locat==y]))
> 1 2 3 4 5 6 7 8 9 10
> 5 15 15 30 30 30 30 15 15 15
>
>
> So why didn't this work? Where's my blind spot? Indeed, why
> did it gives the results it did?
>
> With thanks,
> Ted.
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 13-Mar-08 Time: 14:15:34
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
More information about the R-help
mailing list