Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply() not working when checking column class in a data.frame

Tags:

class

r

apply

I have a dataframe. I want to inspect the class of each column.

x1 = rep(1:4, times=5)
x2 = factor(rep(letters[1:4], times=5))
xdat = data.frame(x1, x2)

> class(xdat)
[1] "data.frame"
> class(xdat$x1)
[1] "integer"
> class(xdat$x2)
[1] "factor"

However, imagine that I have many columns and therefore need to use apply() to help me do the trick. But it's not working.

apply(xdat, 2, class)
         x1          x2 
"character" "character" 

Why cannot I use apply() to see the data type of each column? or What I should do?

Thanks!

like image 281
wen Avatar asked Mar 21 '15 20:03

wen


People also ask

How do I apply a function to each column in a DataFrame in R?

Apply any function to all R data frame You can set the MARGIN argument to c(1, 2) or, equivalently, to 1:2 to apply the function to each value of the data frame. If you set MARGIN = c(2, 1) instead of c(1, 2) the output will be the same matrix but transposed. The output is of class “matrix” instead of “data.

How do I apply a function to all columns?

For this method to work, you first need to select the cells in the column where you want to have the formula. Below are the steps to use the fill down method: In cell A2, enter the formula: =B2*15% Select all the cells in which you want to apply the formula (including cell C2)

How do I find the class of data in R?

To determine the class of any R object's “internal” type, use the class() function. If the object does not have a class attribute, it has an implicit class, prominently “matrix“, “array“, “function” or “numeric,” or the result of typeof(x). The class() function is robust.


1 Answers

You could use

sapply(xdat, class)
#     x1        x2 
# "integer"  "factor" 

using apply would coerce the output to matrix and matrix can hold only a single 'class'. If there are 'character' columns, the result would be a single 'character' class. To understand this check

 str(apply(xdat, 2, I))
 #chr [1:20, 1:2] "1" "2" "3" "4" "1" "2" "3" "4" "1" ...
 #- attr(*, "dimnames")=List of 2
 # ..$ : NULL
 # ..$ : chr [1:2] "x1" "x2"

Now, if we check

 str(lapply(xdat, I))
 #List of 2
 #$ x1:Class 'AsIs'  int [1:20] 1 2 3 4 1 2 3 4 1 2 ...
 #$ x2: Factor w/ 4 levels "a","b","c","d": 1 2 3 4 1 2 3 4 1 2 ...
like image 81
akrun Avatar answered Nov 15 '22 01:11

akrun