I have a dataset looks like this:
df <- data.frame(id = seq(1, 10, by = 1),
group = rep(1:2, each = 5),
r_level = print(c(rep('low', 2), rep('medium', 4), rep('high', 4))),
date = sample(seq(as.Date('2017/02/09'), as.Date('2018/02/09'), by = 'day'), 10),
score = round(rnorm(10, mean = 66, sd = 12), 0),
time_rank = floor(runif(10, min = 1, max = 10)))
I want convert the data type of id, group, r_level, and time_rank to 'factor', and want to avoid duplicating as.factor() function (something like this:
df$id <- as.factor(df$id)
df$group <- as.factor(df$group)
Want to have a convert_dtype() function, that is:
DataFrame <- convert_dtype(DataFrame, ColumnNames, Old_Date_Type, New_Data_Type)
This post Convert type of multiple columns of a dataframe at once might be helpful.
Thanks in advance!