AllenL
2009-Aug-31 16:39 UTC
[R] Simple column selection question- which and character lists
Dear R-list, Seems simple but have tried multiple approaches, no luck. I have a list of column names:>names.species.bio.18=c("Achimillb","Agrosmitb","Amorcaneb","Andrgerab","Ascltubeb","Elymcanab","Koelcrisb","Lespcapib","Liataspeb","Lupipereb","Monafistb","Panivirgb","Petapurpb","Poaprateb","Querellib","Quermacrb","Schiscopb","Sorgnutab")I want to select the column numbers which correspond to these names in my data frame:>which(colnames(data)==names.species.bio.18)Result: +[1] 75 76 +Warning message: +In cols == names.species.bio.18 : + longer object length is not a multiple of shorter object length So I get the first two hits and then it trips an error message. What is >which doing? Why does it seem to have trouble with vectors of characters? My goal is to output the column names/indices which correspond to the columns NOT in the above list, but that is simple once I can find out what they are. Thanks! -Allen -- View this message in context: http://www.nabble.com/Simple-column-selection-question--which-and-character-lists-tp25226500p25226500.html Sent from the R help mailing list archive at Nabble.com.
baptiste auguie
2009-Aug-31 16:48 UTC
[R] Simple column selection question- which and character lists
Hi, I think you want %in% instead of = see ?"%in%" HTH, baptiste 2009/8/31 AllenL <allen.larocque@gmail.com>> > Dear R-list, > Seems simple but have tried multiple approaches, no luck. > > I have a list of column names: > > >names.species.bio.18=c("Achimillb","Agrosmitb","Amorcaneb","Andrgerab","Ascltubeb","Elymcanab","Koelcrisb","Lespcapib","Liataspeb","Lupipereb","Monafistb","Panivirgb","Petapurpb","Poaprateb","Querellib","Quermacrb","Schiscopb","Sorgnutab") > > I want to select the column numbers which correspond to these names in my > data frame: > >which(colnames(data)==names.species.bio.18) > > Result: > +[1] 75 76 > +Warning message: > +In cols == names.species.bio.18 : > + longer object length is not a multiple of shorter object length > > So I get the first two hits and then it trips an error message. > > What is >which doing? Why does it seem to have trouble with vectors of > characters? > My goal is to output the column names/indices which correspond to the > columns NOT in the above list, but that is simple once I can find out what > they are. > > Thanks! > -Allen > > -- > View this message in context: > http://www.nabble.com/Simple-column-selection-question--which-and-character-lists-tp25226500p25226500.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- _____________________________ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
milton ruser
2009-Aug-31 16:50 UTC
[R] Simple column selection question- which and character lists
Hi Allen, have you tryed: newDF<-sourceDF[,names.species.bio.18] This will create a new data.frame with only those species you have interest. By the way, your data.frame is called data? Case yes, avoid this, please. good luck milton On Mon, Aug 31, 2009 at 12:39 PM, AllenL <allen.larocque@gmail.com> wrote:> > Dear R-list, > Seems simple but have tried multiple approaches, no luck. > > I have a list of column names: > > >names.species.bio.18=c("Achimillb","Agrosmitb","Amorcaneb","Andrgerab","Ascltubeb","Elymcanab","Koelcrisb","Lespcapib","Liataspeb","Lupipereb","Monafistb","Panivirgb","Petapurpb","Poaprateb","Querellib","Quermacrb","Schiscopb","Sorgnutab") > > I want to select the column numbers which correspond to these names in my > data frame: > >which(colnames(data)==names.species.bio.18) > > Result: > +[1] 75 76 > +Warning message: > +In cols == names.species.bio.18 : > + longer object length is not a multiple of shorter object length > > So I get the first two hits and then it trips an error message. > > What is >which doing? Why does it seem to have trouble with vectors of > characters? > My goal is to output the column names/indices which correspond to the > columns NOT in the above list, but that is simple once I can find out what > they are. > > Thanks! > -Allen > > -- > View this message in context: > http://www.nabble.com/Simple-column-selection-question--which-and-character-lists-tp25226500p25226500.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
"Stefan Große"
2009-Aug-31 16:58 UTC
[R] Simple column selection question- which and character lists
> I have a list of column names: > >names.species.bio.18=c("Achimillb","Agrosmitb","Amorcaneb","Andrgerab","Ascltubeb","Elymcanab","Koelcrisb","Lespcapib","Liataspeb","Lupipereb","Monafistb","Panivirgb","Petapurpb","Poaprateb","Querellib","Quermacrb","Schiscopb","Sorgnutab") > > I want to select the column numbers which correspond to these names in my > data frame: > >which(colnames(data)==names.species.bio.18)How about: num<-1:length(colnames(data)) num[colnames(data) %in% names.species.bio.18] hth Stefan -- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
Erik Iverson
2009-Aug-31 17:15 UTC
[R] Simple column selection question- which and character lists
1) Don't call your data.frame "data". I will call my "example" one "df". 2) If you want the columns NOT in names.species.bio.18, which is what you said, then the answer is: df[!names(df) %in% names.species.bio.18] Best, Erik -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of AllenL Sent: Monday, August 31, 2009 11:40 AM To: r-help at r-project.org Subject: [R] Simple column selection question- which and character lists Dear R-list, Seems simple but have tried multiple approaches, no luck. I have a list of column names:>names.species.bio.18=c("Achimillb","Agrosmitb","Amorcaneb","Andrgerab","Ascltubeb","Elymcanab","Koelcrisb","Lespcapib","Liataspeb","Lupipereb","Monafistb","Panivirgb","Petapurpb","Poaprateb","Querellib","Quermacrb","Schiscopb","Sorgnutab")I want to select the column numbers which correspond to these names in my data frame:>which(colnames(data)==names.species.bio.18)Result: +[1] 75 76 +Warning message: +In cols == names.species.bio.18 : + longer object length is not a multiple of shorter object length So I get the first two hits and then it trips an error message. What is >which doing? Why does it seem to have trouble with vectors of characters? My goal is to output the column names/indices which correspond to the columns NOT in the above list, but that is simple once I can find out what they are. Thanks! -Allen -- View this message in context: http://www.nabble.com/Simple-column-selection-question--which-and-character-lists-tp25226500p25226500.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.