An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080325/f9065453/attachment.pl
Combining logical operators to extract columns from a dataframe
3 messages · Mark, Erik Iverson, Peter Dalgaard
mtb954 at gmail.com wrote:
Hi R-helpers, I have a dataframe (called data) with 100 columns, the columns of which are named with integers ranging from 1900 to 1999. I wish to extract those columns which names are >=1950 and <=1970. I tried: data2<-subset(data,select=(names(data)>=1950 & names(data)<=1970)) but that doesn't work. Any ideas?
## untested data[as.character(1950:1970)]
Thanks! Mark [[alternative HTML version deleted]]
______________________________________________ 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.
mtb954 at gmail.com wrote:
Hi R-helpers, I have a dataframe (called data) with 100 columns, the columns of which are named with integers ranging from 1900 to 1999. I wish to extract those columns which names are >=1950 and <=1970. I tried: data2<-subset(data,select=(names(data)>=1950 & names(data)<=1970)) but that doesn't work. Any ideas?
The subset() function and in particular its select argument is playing tricks with the evaluation rules, mostly to be helpful, but sometimes apparently not... Ordinary indexing should work: data[, names(data) >= "1950" & names(data) <= "1970"] (using quoted strings to emphasize that you are really comparing alphabetically. That is probably OK here, but beware that > "999" > 1999 [1] TRUE Also, BTW, check that names really are "1950", not "X1950" or so. Data frame methods like to rationalize column names to be valid variable names.)
Thanks! Mark [[alternative HTML version deleted]]
______________________________________________ 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.
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 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907