Quantcast

rownames in an apply function

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

rownames in an apply function

Santosh Srinivas
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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: rownames in an apply function

David Winsemius

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: rownames in an apply function

Santosh Srinivas
Thanks David. Is there a way of explicitly passing the rowname since I
need it in the function.

On Apr 19, 4:19 pm, David Winsemius <[hidden email]> wrote:

> 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(1303185270begin_of_the_skype_highlighting            1303185270      end_of_the_skype_highlighting,1303185300begin_of_the_skype_highlighting            1303185300      end_of_the_skype_highlighting,1303185330begin_of_the_skype_highlighting            1303185330      end_of_the_skype_highlighting,
> >1303185360begin_of_the_skype_highlighting            1303185360      end_of_the_skype_highlighting,
> >1303185390begin_of_the_skype_highlighting            1303185390      end_of_the_skype_highlighting), 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
>
> ______________________________________________
> [hidden email] mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: rownames in an apply function

Gabor Grothendieck
In reply to this post by Santosh Srinivas
On Tue, Apr 19, 2011 at 6:59 AM, santosh <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: rownames in an apply function

Santosh Srinivas
Thanks.

On Tue, Apr 19, 2011 at 5:16 PM, Gabor Grothendieck
<[hidden email]> wrote:

> On Tue, Apr 19, 2011 at 6:59 AM, santosh <[hidden email]> 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
>

______________________________________________
[hidden email] 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.
Loading...