Skip to content

Combining logical operators to extract columns from a dataframe

3 messages · Mark, Erik Iverson, Peter Dalgaard

#
mtb954 at gmail.com wrote:
## untested

data[as.character(1950:1970)]
#
mtb954 at gmail.com wrote:
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.)