0

I have an issue in converting data into the numeric format.

str(DfFilter)

output

'data.frame':   32 obs. of  5 variables:
 $ InstanceType      : chr  "  c1.xlarge" "  c1.xlarge" "  c1.xlarge" "  c1.xlarge" ...
 $ ProductDescription: chr  "  Linux/UNIX" "  Linux/UNIX" "  Linux/UNIX" "  Linux/UNIX" ...
 $ SpotPrice         : num  0.052 0.0739 0.0747 0.0751 0.0755 ...
 $ ymd_hms(Timestamp): POSIXct, format: "2021-05-16 06:26:40" "2021-05-16 00:58:55" "2021-05-16 06:46:50" ...
 $ Timestamp         : 'times' num  06:26:40 00:58:55 06:46:50 14:17:55 19:07:09 ...
  ..- attr(*, "format")= chr "h:m:s"

but when i run to check for numeric values as follow

is.numeric(DfFilter)
[1] FALSE

why is that so. Kindly help in understanding this issue. Thanks in advance.

NIrbhay Mathur
  • 153
  • 1
  • 1
  • 10

2 Answers2

2

With purrr package and based on the comments:

DfModel <- DfFilter %>% 
  purrr::keep(.p = function(x) is.numeric(x))

It will keep only the numeric variables

MonJeanJean
  • 2,876
  • 1
  • 4
  • 20
1

Filter with is.numeric could be used to get only numeric columns.

Filter(is.numeric, DfFilter)
#  a   c
#1 1 2.2

Another way to keep only numeric value in a data.frame the result of is.numeric used in sapply could be used for subsetting with [:

DfFilter[sapply(DfFilter, is.numeric)]
#  a   c
#1 1 2.2

Example dataset:

DfFilter <- data.frame(a=1, b="b", c=2.2)
GKi
  • 37,245
  • 2
  • 26
  • 48