[R] rownames in an apply function

David Winsemius dwinsemius at comcast.net
Tue Apr 19 13:19:35 CEST 2011


On Apr 19, 2011, at 6:59 AM, santosh wrote:

> Dear Group,
>
> I am doing an apply function on a zoo object. I need the rownames to
> be passed to the function.
> Any suggestions on how to accomplish this?
>
> Here is the code I am using
>
> tmp <-
> structure(c(611.55, 611.55, 611.55, 611.55, 611.55, 520, 520,
> 520, 520, 520, 425, 425, 425, 432.2, 432.2, 337, 338, 328.95,
> 328.95, 328.95, 243.8, 242.775, 238.5, 239, 235.925, 161.7, 156.625,
> 152.825, 151.95, 151.9, 1.5, 1.5, 1.5, 1.5, 1.5, 2, 2.15, 2.15,
> 2, 2, 3, 2.95, 3.175, 3.2, 3.15, 6, 6.15, 6.525, 6.6, 6.6, 14.5,
> 14.5, 15.15, 15.45, 15.3, 32.05, 32.05, 33.09, 33.8, 33.23), .Dim =
> c(5L,
> 12L), .Dimnames = list(c("2011-04-19 09:24:30", "2011-04-19
> 09:25:00",
> "2011-04-19 09:25:30", "2011-04-19 09:26:00", "2011-04-19 09:26:30"
> ), c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"
> )), index = structure(c(1303185270, 1303185300, 1303185330,
> 1303185360,
> 1303185390), class = c("POSIXt", "POSIXct"), tzone = ""), class =
> "zoo")
>
>
>> tmp
>                      A   B   C   D   E   F   G    H    I    J    K
> L
> 2011-04-19 09:24:30 612 520 425 337 244 162 1.5 2.00 3.00 6.00 14.5
> 32.0
>
snipped
>
>> apply(tmp,1,function(x)(print (rownames(x))))
> NULL
> NULL
snipped
>> #How can I get the rownames here instead?

 > rownames(tmp)
[1] "2011-04-19 09:24:30" "2011-04-19 09:25:00"
[3] "2011-04-19 09:25:30" "2011-04-19 09:26:00"
[5] "2011-04-19 09:26:30"

(apply() removes the rownames before passing rows on to functions, ...  
since they are no longer rows but rather vectors at that point.)

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list