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")]
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 at 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
Hello,
I dont clearly see why you have a problem... What you have to us works
perfectly.
?? mat=diag(3)
?? colnames(mat)=letters[1:3]
?? mat
a b c
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
?? mat[,c("a","c")]
a c
[1,] 1 0
[2,] 0 0
[3,] 0 1
Only condition is to have *set* the names if those columns. See
'colnames'
or 'dimnames' for that.
Eric
At 16:19 27/05/2004, you 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")]
>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 at 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 at 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
Eric Lecoutre
UCL / Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium
tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte
?subset *********** REPLY SEPARATOR *********** On 5/27/2004 at 5:19 PM 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")] > > >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 at 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 at 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.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
Dear Adrian,
you could use something like:
x <- matrix(1:30,5,6)
colnames(x) <- letters[1:6]
your.choices <- c("a", "d", "f")
x[,match(your.choices, colnames(x))]
I hope this helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Adrian Dusa" <adi at roda.ro>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, May 27, 2004 4:19 PM
Subject: [R] extract columns using their names
> 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")]
>
>
> 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 at 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 at 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
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)) > data1alfa 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 at 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 at 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.htmlPetr Pikal petr.pikal at precheza.cz