Mauricio Cornejo
2011-Jun-09 19:34 UTC
[R] How to subset based on column name that is a number ?
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? Many thanks, Mauricio [[alternative HTML version deleted]]
Jorge Ivan Velez
2011-Jun-09 20:12 UTC
[R] How to subset based on column name that is a number ?
Hi Mauricio, Try the following: # using the iris data require(MASS) x <- iris x[x[, 5] == 'setosa',] # and your data dframe[dframe[, 1] == "FY11_Q4", ] HTH, Jorge On Thu, Jun 9, 2011 at 3:34 PM, 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? > > Many thanks, > Mauricio > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
peter dalgaard
2011-Jun-09 20:35 UTC
[R] How to subset based on column name that is a number ?
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
Petr PIKAL
2011-Jun-10 11:27 UTC
[R] Odp: How to subset based on column name that is a number ?
Hi> [R] How to subset based on column name that is a number ? > > Hi, > > I have a data frame with column names "1", "2", "3", ... and I'd like toextract> a subset based on the values in the first column. None of the methods Itried> 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",] >Did you try dframe[dframe[,"1"]=="My Text",] Regards Petr> > Might anyone have a suggestion? > > Many thanks, > Mauricio > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Maybe Matching Threads
- How to use access results of gregexpr in data frames
- How to use 'switch' with strings containing spaces?
- Problems understanding use of regular expression (in gsub) for manipulating currency
- Inexplicably different results using subset vs bracket notation on logical variable
- select columns from vector of column names