-1

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!

AI2.0
  • 313
  • 1
  • 3
  • 7
  • You should stop posting R code as snippets. Nothing useful happens. Just adds clutter. Instead use the code block format icon "{}" in the editing tools. – IRTFM Feb 09 '18 at 21:26

1 Answers1

2

Just use lapply on the part of the dataframe required and assign back to those columns:

 df[ ,  c('id', 'group', 'r_level', 'time_rank')] <- 
            lapply( df[ ,  c('id', 'group', 'r_level', 'time_rank')], factor)

Check result:

str(df)
'data.frame':   10 obs. of  6 variables:
 $ id       : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10
 $ group    : Factor w/ 2 levels "1","2": 1 1 1 1 1 2 2 2 2 2
 $ r_level  : Factor w/ 3 levels "high","low","medium": 2 2 3 3 3 3 1 1 1 1
 $ date     : Date, format: "2017-11-01" "2018-01-21" "2017-12-10" ...
 $ score    : num  59 73 67 69 68 48 71 60 43 68
 $ time_rank: Factor w/ 6 levels "1","3","6","7",..: 1 5 6 5 1 2 6 4 3 6
IRTFM
  • 258,963
  • 21
  • 364
  • 487