-1

What is the command to check which format a column within a dataframe is, i.e., numeric, char, factorial, etc.

I have searched this online; apparently I am using the wrong search terms.

Ferdi
  • 540
  • 3
  • 12
  • 23
Collective Action
  • 7,607
  • 15
  • 45
  • 60

2 Answers2

1

Easy.

class(df$yourcol
#i.e.
df<-data.frame(matrix(1:4,2,2))
class(df$X2)

Edit

As Sotos commented above you can also do str(df) which returns a class for all columns.

CCurtis
  • 1,770
  • 3
  • 15
  • 25
1

@CCurtis answer is correct, but you can also use the command sapply(df, class)

In the following example I use the diamonds dataset from the ggplot2 package.

library(ggplot2)
sapply(diamonds, class)

gives you the following output

$carat
[1] "numeric"

$cut
[1] "ordered" "factor" 

$color
[1] "ordered" "factor" 

$clarity
[1] "ordered" "factor" 

$depth
[1] "numeric"

$table
[1] "numeric"

$price
[1] "integer"

$x
[1] "numeric"

$y
[1] "numeric"

$z
[1] "numeric"
Ferdi
  • 540
  • 3
  • 12
  • 23