I convert my columns data type manually:
data[,\'particles\'] <- as.numeric(as.character(data[,\'particles\']))
This not ideal as the da
If you don't know which columns need to be converted beforehand, you can extract that info from your dataframe as follows:
vec <- sapply(dat, is.factor)
which gives:
> vec
particles humidity timestamp date
TRUE TRUE FALSE FALSE
You can then use this vector to do the conversion on the subset with lapply:
# notation option one:
dat[, vec] <- lapply(dat[, vec], function(x) as.numeric(as.character(x)))
# notation option two:
dat[vec] <- lapply(dat[vec], function(x) as.numeric(as.character(x)))
If you want to detect both factor and character columns, you can use:
sapply(dat, function(x) is.factor(x)|is.character(x))