I have a data frame, df
, which has columns of different class:
df <- structure(list(ID = 1:6, weight = c(74, 60, 49, 75, 75, 57),
date = structure(c(1488931200, 1501459200, 1493251200, 1494201600,
1494201600, 1493251200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
group = c("B", "B", "A", "A", "A", "A")), row.names = c(NA,
6L), class = "data.frame")
df
ID weight date group
1 1 74 2017-03-08 B
2 2 60 2017-07-31 B
3 3 49 2017-04-27 A
4 4 75 2017-05-08 A
5 5 75 2017-05-08 A
6 6 57 2017-04-27 A
> sapply(df, class)
$ID
[1] "integer"
$weight
[1] "numeric"
$date
[1] "POSIXct" "POSIXt"
$group
[1] "character"
I want to be able to select which columns have a certain class. For example:
names(which(sapply(df, class) == "numeric" | sapply(df, class) == "character"))
[1] "weight" "group"
However, this doesn't work with class of the date column.
dput(class(df$date))
c("POSIXct", "POSIXt")
names(which(sapply(df, class) == c("POSIXct", "POSIXt")))
character(0)
Can someone advise the best way to give me the name of columns that have the POSIXct class?