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))
Use lapply
:
cols <- c("particles", "nox", ...)
data[,cols] <- lapply(data[,cols], function(x) as.numeric(as.character(x)))
Another option using mutate_if()
from dplyr
which allows you to operate on columns for which a predicate returns TRUE
library(dplyr)
df %>%
mutate_if(is.factor, funs(as.numeric(as.character(.))))
Note: This method will work for your follow up question as well
We can use data.table
library(data.table)
setDT(df)[, lapply(.SD, function(x) if(is.factor(x)) as.numeric(as.character(x)) else x)]
The best option is I think apply
You can do
newD<-apply(data[,"names"], 2,function(x) as.numeric(as.character(x)))
where in "names" you put all the variables you want. Then apply with 2 as second argument will apply the function(x) on all the columns(if you put 1 its by rows) of the first argument. And you can save it as new dataset or rewrite the old one with
data[,"names"]<-apply....