Re: [R] rownames in an apply function

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 19 Apr 2011 07:46:51 -0400

On Tue, Apr 19, 2011 at 6:59 AM, santosh <santosh.srinivas_at_gmail.com> 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
> 2011-04-19 09:25:00 612 520 425 338 243 157 1.5 2.15 2.95 6.15 14.5
> 32.0
> 2011-04-19 09:25:30 612 520 425 329 238 153 1.5 2.15 3.17 6.53 15.2
> 33.1
> 2011-04-19 09:26:00 612 520 432 329 239 152 1.5 2.00 3.20 6.60 15.4
> 33.8
> 2011-04-19 09:26:30 612 520 432 329 236 152 1.5 2.00 3.15 6.60 15.3
> 33.2
>
>> apply(tmp,1,function(x)(print (rownames(x))))
> NULL
> NULL
> NULL
> NULL
> NULL
> NULL
>>#How can I get the rownames here instead?

Try this:

sapply(time(tmp), function(tt) rownames(tmp[tt]))

# however, it may be that what you really want is # the times and not the rownames:

lapply(time(tmp), function(tt) tt)

# also note that indexing by time works

sapply(time(tmp), function(tt) tmp[tt])

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
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.
Received on Tue 19 Apr 2011 - 11:49:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 19 Apr 2011 - 12:10:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive