[R] How to subset based on column name that is a number ?

peter dalgaard pdalgd at gmail.com
Thu Jun 9 22:35:01 CEST 2011


On Jun 9, 2011, at 21:34 , Mauricio Cornejo wrote:

> Hi,
> 
> I have a data frame with column names "1", "2", "3", ... and I'd like to extract 
> a subset based on the values in the first column.  None of the methods I tried 
> worked (below).
> 
> 
> x <- subset(dframe, 1 = = "My Text")
> x <- subset(dframe, "1" = = "My Text")
> x <- subset(dframe, names(dframe)[1] = = "My Text")
> Q <- dframe[1 = = "FY11_Q4",]
> Q <- dframe['1'=="FY11_Q4",]
> Q <- dframe[names(dframe)[1]=="FY11_Q4",]
> 
> 
> Might anyone have a suggestion?

x  <- subset(dframe, `1` == "My Text")
dframe[dframe[,'1']=="FY11_Q4",]
dframe[dframe[,1]=="FY11_Q4",]

(These are of course only equivalent if it is the 1st column that is named "1"...)

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list