Automatically check quantitative variables in R

Asked

Viewed 196 times

3

I have a database here and I need to automatically check which quantitative variables are. My advisor says I need to use sapply and the function is.numeric and create a code that returns a vector of true and false indicating which variables are numeric.

Could you help me ?

1 answer

1

Yes, you will use the sapply with the function is.numeric. In summary, the sapply will apply to each variable (column) of your data.frame the function is.numeric to know if that is a numeric variable (column).

Generating a basis for demonstration:

df <- data.frame(numeric1 = 1:10,
                 factor = factor(c("a", "b")),
                 character = "texto",
                 numeric2 = 10:1,
                 logical = TRUE ,
                 stringsAsFactors = FALSE)
str(df)
 $ numeric1 : int  1 2 3 4 5 6 7 8 9 10
 $ factor   : Factor w/ 2 levels "a","b": 1 2 1 2 1 2 1 2 1 2
 $ character: chr  "texto" "texto" "texto" "texto" ...
 $ numeric2 : int  10 9 8 7 6 5 4 3 2 1
 $ logical  : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...

Using the sapply with is.numeric to generate a vector indicating which columns are numerical:

numericas <- sapply(df, is.numeric)
numericas
numeric1    factor character  numeric2   logical 
     TRUE     FALSE     FALSE      TRUE     FALSE

Now you can use this vector to filter the data.frame:

df[,numericas]
  numeric1 numeric2
1         1       10
2         2        9
3         3        8
4         4        7
5         5        6
6         6        5
7         7        4
8         8        3
9         9        2
10       10        1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.