Home > other >  Is there any method to choose one character(certain) and only numeric columns from data.table?
Is there any method to choose one character(certain) and only numeric columns from data.table?

Time:01-16

A certain character column(emp_name) and only numeric columns must be selected from a data base. I tried .SDcols function, bu I could not figure out. Here is data.table

emp.data <- data.table(

emp_id = c (1:5),

emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),

salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")),

stringsAsFactors = FALSE )

CodePudding user response:

We may use a function in .SDcols

emp.data[, .SD, .SDcols = function(x) is.numeric(x)|is.character(x)]

-output

   emp_id emp_name salary
1:      1     Rick 623.30
2:      2      Dan 515.20
3:      3 Michelle 611.00
4:      4     Ryan 729.00
5:      5     Gary 843.25

Or may also use

emp.data[, c(.(emp_name = emp_name),.SD), .SDcols = is.numeric]

-output

    emp_name emp_id salary
1:     Rick      1 623.30
2:      Dan      2 515.20
3: Michelle      3 611.00
4:     Ryan      4 729.00
5:     Gary      5 843.25

Or as the only column removed is Date, we can also negate (!)

emp.data[, .SD, .SDcols = !is_date]
  emp_id emp_name salary
1:      1     Rick 623.30
2:      2      Dan 515.20
3:      3 Michelle 611.00
4:      4     Ryan 729.00
5:      5     Gary 843.25
  • Related