I want to filter columns of a data.table based on their attribute. The answer is actually based on Nera's answer here Convert column classes in data.table
# example
DT <- data.table(x = c("a", "b", "c"),
y = c(1L, 2L, 3L),
z = c(1.1, 2.1, 3.1))
# filter by class with nested structure
changeCols <- colnames(DT)[which(as.vector(DT[,lapply(.SD, class)]) == "character")]
changeCols
# filter by class with pipeline
DT[,lapply(.SD, class)] %>%
as.vector(.) == "character" %>%
which(.)
# Error in which(.) : argument to 'which' is not logical
# simply break the pipeline corrects the error
cols <- DT[,lapply(.SD, class)] %>% as.vector(.) == "character"
which(cols)
I don't understand why there's an error when I use pipeline while the error was solved when breaking the pipe. Thanks!