Re: [R] rownames in an apply function

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 19 Apr 2011 07:19:35 -0400

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

______________________________________________
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:22:13 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 - 11:50: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