0

I have a table with lots of data and need the variable type of specific columns. The function sapply brings the variable type for ALL columns while I want specific ones. Can you help? Thanks!

  • 1
    You can select a single column with `[[` (with a quoted column name or string) or `$` (unquoted column name). E.g., `typeof(your_data[["specific column"]])` or `typeof(your_data$specific_column)`. Or whatever function you want, `class`, `mode`, `sum`, `mean`, ... – Gregor Thomas Sep 13 '19 at 19:23
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 13 '19 at 19:41

2 Answers2

0

You can use:

a <- data.frame(a = c('a', 'b'), b = c(1L, 2L), c = c(1.,2.), d = c(TRUE, FALSE),
                stringsAsFactors = FALSE)

# populate with specific column names
col_names <- c('a','b')
sapply(a[,col_names], typeof)
sapply(a[,col_names], class)
# ... and other functions
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
0

Supposing you have:

x<-data.frame(column1=c("a","b","c"),column2=c(1,2,3))

If I understand your question, you can do:

class(x[,2])

to determine the class of the second column.

DrDominio
  • 113
  • 1
  • 10