Re: [R] extract columns using their names

About this list Date view Thread view Subject view Author view Attachment view

From: Petr Pikal (petr.pikal@precheza.cz)
Date: Fri 28 May 2004 - 00:32:35 EST


Message-id: <40B61823.5502.1E6D3AF@localhost>


On 27 May 2004 at 17:19, Adrian Dusa wrote:

> Hello,
>
> Is there a way to extract multiple columns from a dataframe using
> their names instead of their numbers?
>
> Currently I use:
>
> data2 <- data1[, c(1,3,9)]
>
> And I am looking for something like
>
> data2 <- data1[, c("XX","YY","ZZ")]

Hi

Did you try it? Works on R 1.9.0 W2000

> data1<-data.frame(alfa=rnorm(10),beta=rnorm(10,5),gama=rnorm(10,.1))
> data1
          alfa beta gama
1 -0.68536013 5.310451 1.55570932
2 -0.43824815 4.998961 0.51348779
3 -1.20083111 4.585788 0.68135905
4 -0.07962607 6.443884 0.05560703
5 -0.03044263 5.355516 -0.16195279
6 -0.19878437 4.142789 0.07888565
7 1.90507619 4.691988 0.88832135
8 -0.51817154 4.844728 -0.80310925
9 -1.19614142 4.457188 -1.00103103
10 0.50957999 6.178542 -1.06628714
> data1[,c("alfa","gama")]
          alfa gama
1 -0.68536013 1.55570932
2 -0.43824815 0.51348779
3 -1.20083111 0.68135905
4 -0.07962607 0.05560703
5 -0.03044263 -0.16195279
6 -0.19878437 0.07888565
7 1.90507619 0.88832135
8 -0.51817154 -0.80310925
9 -1.19614142 -1.00103103
10 0.50957999 -1.06628714

Cheers
Petr
>
>
> I use the same dataframe for many purposes, and I run codes that
> change the order of the columns every time.
>
> Many thanks,
> Adrian
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Adrian Dusa (adi@roda.ro)
> Romanian Social Data Archive (www.roda.ro)
> 1, Schitu Magureanu Bd.
> 050025 Bucharest sector 5
> Romania
> Tel./Fax: +40 (21) 312.66.18\
> +40 (21) 312.02.10/ int.101
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal@precheza.cz

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:13 EST