changing names of vectors in list or data.frame

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

changing names of vectors in list or data.frame

Clint Harshaw
When I combine separate vectors into one list, there are new names
created. I'd like to change them to something more meaningful.

Here are two examples using data(zelazo) from library(ISwR):

 > library(ISwR)
 > data(zelazo)
 > attach(zelazo)
 > zelazo
$active
[1]  9.00  9.50  9.75 10.00 13.00  9.50

$passive
[1] 11.00 10.00 10.00 11.75 10.50 15.00

$none
[1] 11.50 12.00  9.00 11.50 13.25 13.00

$ctr.8w
[1] 13.25 11.50 12.00 13.50 11.50
 > walk <- stack(list("active"=active, "passive"=passive, "none"=none,
"ctr.8w"=ctr.8w))
 > walk
    values     ind
1    9.00  active
[...rows deleted...]
23  11.50  ctr.8w

I want to name the first column "walking" and the second column
"training". How do I do this?

Here is a second example:

 > walk2 <- data.frame(c(active, passive, none, ctr.8w), c(rep(1:4,
c(length(active), length(passive), length(none), length(ctr.8w)))))
 > walk2
    c.active..passive..none..ctr.8w.
1                              9.00
[...rows deleted...]
23                            11.50
 
c.rep.1.4..c.length.active...length.passive...length.none...length.ctr.8w....
1
        [...rows deleted...]
4
 >

The created names are very long. Again, I want the name of the first
column to be "walking" and the second column to be "training".

Thanks,
Clint

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: changing names of vectors in list or data.frame

Prof Brian Ripley
1) use names() on the resulting data frame: stack() always uses those two
names.  As in

names(walk) <- c("walking", "training")

2) data.frame is more useful when the arguments are named, and you can
just name them: see the help.

On Sun, 19 Feb 2006, Clint Harshaw wrote:

> When I combine separate vectors into one list, there are new names
> created. I'd like to change them to something more meaningful.
>
> Here are two examples using data(zelazo) from library(ISwR):
>
> > library(ISwR)
> > data(zelazo)
> > attach(zelazo)
> > zelazo
> $active
> [1]  9.00  9.50  9.75 10.00 13.00  9.50
>
> $passive
> [1] 11.00 10.00 10.00 11.75 10.50 15.00
>
> $none
> [1] 11.50 12.00  9.00 11.50 13.25 13.00
>
> $ctr.8w
> [1] 13.25 11.50 12.00 13.50 11.50
> > walk <- stack(list("active"=active, "passive"=passive, "none"=none,
> "ctr.8w"=ctr.8w))
> > walk
>    values     ind
> 1    9.00  active
> [...rows deleted...]
> 23  11.50  ctr.8w
>
> I want to name the first column "walking" and the second column
> "training". How do I do this?
>
> Here is a second example:
>
> > walk2 <- data.frame(c(active, passive, none, ctr.8w), c(rep(1:4,
> c(length(active), length(passive), length(none), length(ctr.8w)))))
> > walk2
>    c.active..passive..none..ctr.8w.
> 1                              9.00
> [...rows deleted...]
> 23                            11.50
>
> c.rep.1.4..c.length.active...length.passive...length.none...length.ctr.8w....
> 1
>        [...rows deleted...]
> 4
> >
>
> The created names are very long. Again, I want the name of the first
> column to be "walking" and the second column to be "training".
>
> Thanks,
> Clint
>
> ______________________________________________
> [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
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: changing names of vectors in list or data.frame

Peter Dalgaard
In reply to this post by Clint Harshaw
Clint Harshaw <[hidden email]> writes:

> When I combine separate vectors into one list, there are new names
> created. I'd like to change them to something more meaningful.
>
> Here are two examples using data(zelazo) from library(ISwR):
>
>  > library(ISwR)
>  > data(zelazo)
>  > attach(zelazo)
>  > zelazo
> $active
> [1]  9.00  9.50  9.75 10.00 13.00  9.50
>
> $passive
> [1] 11.00 10.00 10.00 11.75 10.50 15.00
>
> $none
> [1] 11.50 12.00  9.00 11.50 13.25 13.00
>
> $ctr.8w
> [1] 13.25 11.50 12.00 13.50 11.50
>  > walk <- stack(list("active"=active, "passive"=passive, "none"=none,
> "ctr.8w"=ctr.8w))
>  > walk
>     values     ind
> 1    9.00  active
> [...rows deleted...]
> 23  11.50  ctr.8w
>
> I want to name the first column "walking" and the second column
> "training". How do I do this?

The obvious way would seem to be

names(walk) <- c("walking","training")
 

> Here is a second example:
>
>  > walk2 <- data.frame(c(active, passive, none, ctr.8w), c(rep(1:4,
> c(length(active), length(passive), length(none), length(ctr.8w)))))
>  > walk2
>     c.active..passive..none..ctr.8w.
> 1                              9.00
> [...rows deleted...]
> 23                            11.50
>  
> c.rep.1.4..c.length.active...length.passive...length.none...length.ctr.8w....
> 1
>         [...rows deleted...]
> 4
>  >
>
> The created names are very long. Again, I want the name of the first
> column to be "walking" and the second column to be "training".

walk2 <- data.frame(walking=c(active, passive, none, ctr.8w),
   training=c(rep(1:4, c(length(active), length(passive),
   length(none), length(ctr.8w)))))


[or
 walk2 <- data.frame(walking=unlist(zelazo),
                     training=factor(rep(1:4, sapply(zelazo,length)),
                                     labels=names(zelazo) ))
]
--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[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