Skip to content

Extracting Numeric Columns from Data Fram

7 messages · Barry DeCicco, Rui Barradas, Marc Schwartz +3 more

#
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:
#
On Feb 16, 2013, at 12:15 PM, Barry DeCicco <bdecicco2001 at yahoo.com> wrote:

            
Something 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:

            
#
http://stackoverflow.com/questions/5863097/selecting-only-numeric-columns-from-a-data-frame

John Kane
Kingston ON Canada
____________________________________________________________
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.