Hello, I've got a data frame with a mix of numeric, integer and factor columns. I'd like to pull out (or just operate only on) the numeric/integer columns. Every thing I've found in searches is about how to subset by rows, or how to operate assuming you have the column names. I'd like to pull by type. Thanks! Barry [[alternative HTML version deleted]]
Hello, You should provide us with a data example, like the posting guide says. Anyway, see the following example. # make up some data dat <- data.frame(X = 1:4, Y = rnorm(4), Z = letters[1:4]) str(dat) # this returns the numeric/integer columns dat[sapply(dat, is.numeric)] Hope this helps, Rui Barradas Em 16-02-2013 18:15, Barry DeCicco escreveu:> Hello, > > I've got a data frame with a mix of numeric, integer and factor columns. > I'd like to pull out (or just operate only on) the numeric/integer columns. > Every thing I've found in searches is about how to subset by rows, > or how to operate assuming you have the column names. I'd like to pull > by type. > > Thanks! > > Barry > > [[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. >
On Feb 16, 2013, at 12:15 PM, Barry DeCicco <bdecicco2001 at yahoo.com> wrote:> Hello, > > I've got a data frame with a mix of numeric, integer and factor columns. > I'd like to pull out (or just operate only on) the numeric/integer columns. > Every thing I've found in searches is about how to subset by rows, > or how to operate assuming you have the column names. I'd like to pull > by type. > > Thanks! > > BarrySomething like the following should work. If your data frame is "DF": NewDF <- DF[, sapply(DF, is.numeric)] Regards, Marc Schwartz
Barry Suppose your data frame is called "mydat". Then something like mydat[,sapply(mydat,class) %in% c('numeric','integer')] might do what you want. - Phil On Sat, 16 Feb 2013, Barry DeCicco wrote:> Hello, > > I've got a data frame with a mix of numeric, integer and factor columns. > I'd like to pull out (or just operate only on) the numeric/integer columns. > Every thing I've found in searches is about how to subset by rows, > or how to operate assuming you have the column names.? I'd like to pull > by type. > > Thanks! > > Barry > > [[alternative HTML version deleted]] > >
http://stackoverflow.com/questions/5863097/selecting-only-numeric-columns-from-a-data-frame John Kane Kingston ON Canada> -----Original Message----- > From: bdecicco2001 at yahoo.com > Sent: Sat, 16 Feb 2013 10:15:35 -0800 (PST) > To: r-help at r-project.org > Subject: [R] Extracting Numeric Columns from Data Fram > > Hello, > > I've got a data frame with a mix of numeric, integer and factor columns. > I'd like to pull out (or just operate only on) the numeric/integer > columns. > Every thing I've found in searches is about how to subset by rows, > or how to operate assuming you have the column names. I'd like to pull > by type. > > Thanks! > > Barry > > [[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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Hi, set.seed(15) dat1<-data.frame(col1=rnorm(6),col2=rep(1:2,each=3),col3=rep(letters[1:3],2),col4=runif(6),col5=rep(LETTERS[3:5],2)) dat1[,sapply(dat1,class)!="factor"] #??????? col1 col2????? col4 #1? 0.2588229??? 1 0.5090904 #2? 1.8311207??? 1 0.7066286 #3 -0.3396186??? 1 0.8623137 #4? 0.8971982??? 2 0.8417851 #5? 0.4880163??? 2 0.4474437 #6 -1.2553858??? 2 0.9646670 #or ?dat1[,grep("numeric|integer",sapply(dat1,class))] #??????? col1 col2????? col4 #1? 0.2588229??? 1 0.5090904 #2? 1.8311207??? 1 0.7066286 #3 -0.3396186??? 1 0.8623137 #4? 0.8971982??? 2 0.8417851 #5? 0.4880163??? 2 0.4474437 #6 -1.2553858??? 2 0.9646670 A.K. ----- Original Message ----- From: Barry DeCicco <bdecicco2001 at yahoo.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Saturday, February 16, 2013 1:15 PM Subject: [R] Extracting Numeric Columns from Data Fram Hello, I've got a data frame with a mix of numeric, integer and factor columns. I'd like to pull out (or just operate only on) the numeric/integer columns. Every thing I've found in searches is about how to subset by rows, or how to operate assuming you have the column names.? I'd like to pull by type. Thanks! Barry ??? [[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.
Dear Barry, I saw that you received several nice answers on how to 'pull out' numeric columns. You also wrote that an alternative could be to 'just operate only on' numerics. Here is one possibility: library(plyr) # some dummy data df <- data.frame(nonnum1 = letters[1:5], num1 = 1:5, nonnum2 = letters[6:10], num2 = rnorm(5)) # operate only on numeric variables numcolwise(.fun = mean)(df) # or only on 'discrete' variables (following the terminology on ?colwise) catcolwise(.fun = toupper)(df) Best regards, Henrik Hello, I've got a data frame with a mix of numeric, integer and factor columns. I'd like to pull out (or just operate only on) the numeric/integer columns. Every thing I've found in searches is about how to subset by rows, or how to operate assuming you have the column names.? I'd like to pull by type. Thanks! Barry -- Henrik P?rn Centre for Conservation Biology Department of Biology Norwegian University of Science and Technology NO-7491 Trondheim NORWAY Office: +47 735 96084 Mobile: +47 909 89 255 Fax: +47 735 96100