[R] access/row access/col access

David Winsemius dwinsemius at comcast.net
Fri Jan 13 19:34:49 CET 2012


On Jan 13, 2012, at 10:45 AM, statquant2 wrote:

> Hello,
> I have a data.frame and I want to transfor it in a list of rows or  
> columns.
> I can do apply(myDataFrame,MARGIN=1,FUN=???)
>
> I remember that there is a function which mean return or access  
> column ...
> something like "::" or "]," or "[,"
> I can't remember can somebody refresh my memory?

It is definitely "the long way around) to do this, since [,5] would  
get the same results, but if you want the fifth element in each row  
you could do this:

 > dat
   id x1 x2 x3 y1 y2 y3  z1  z2  z3   v
1  1  2  4  5 10 20 15 200 150 170 2.5
2  2  3  7  6 25 35 40 300 350 400 4.2
 > apply(dat, 1, "[", 5)
[1] 10 25

When 'apply' is used, the trailing arguments are matched either by  
position or name to the arguments of the function. In this case the 5  
gets matched to the "i" for the "[" function. Because "[" is primitive  
the name is actually ignored even if offered, so using any other name  
would not change the result:

 > apply(dat, 1, "[", j=5)
[1] 10 25

Whereas if you were using quantile as your function, you might perhaps  
use prob=c(,25,,75), na.rm=TRUE or na.rm=TRUE prob=c(,25,,75), prob=c(, 
25,,75)

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list