[R] a more elegant way to get percentages?

Erik Iverson iverson at biostat.wisc.edu
Thu Mar 13 15:59:50 CET 2008


Ted -

(Ted Harding) wrote:
> Now that people have answered Monica's query, can someone help me?!!
> See below.
> 
> 
> 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
> 
> So why didn't this work? Where's my blind spot? Indeed, why
> did it gives the results it did?

Remember that apply with margin = 1 will operate row-wise on an *array*, 
a common example being a matrix.

'x' in this case is not an array, it is a data.frame.  It is therefore 
coerced to an array as this section of ?apply says,

    If 'X' is not an array but has a dimension attribute, 'apply'
      attempts to coerce it to an array via 'as.matrix' if it is
      two-dimensional (e.g., data frames) or via 'as.array'.

Therefore, your anonymous function is receiving a character vector made 
up of each row of the data.frame, something like a named vector with 
(for example),

locat = "a"
val   = "5"

Then, your "==" comparison is comparing a factor of length 10 with 
c("a", "5") so vector recycling rules apply, and things go awry from there.

To illustrate, in the second call of the apply function (i.e., second row)

 > x[,1]
  [1] a b b c c c c d d d
Levels: a b c d

 > y
locat   val
   "b"  " 5"

 > x[,1] == y
  [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Thus, the second "b" is all that gets picked up, which has a val of 15.

Hope that helps,
Erik Iverson
> 
> 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.



More information about the R-help mailing list