I have a dataframe named df with 70 character variables in. I am trying to create a function to turn all these character columns into categorical variables without having to specify each column name. An example of this is as such:
df
  fruits   cars 
1 apple    volvo
2 pear     bwm
3 apple    bwm
4 orange   volvo
5 orange   fiat
And my desired output looks as such:
df
  fruits   cars 
1 1        1
2 2        2
3 1        2
4 3        1
5 3        3
I have tried converting to factor and then specifying the levels which has worked when performing on a single column by not using apply. Here was my attempt:
x <- apply(df$fruit, 2, factor)
levels(x) <- 1:length(levels(x))
Failing when in a function
label_num <- function(x){
assigned <- 1:length(levels(x))
return(assigned)
}
x <- apply(df, 2, factor)
apply(levels(x), 2, label_num)
I receive the following error:
Error in apply(levels(x), 2, label_num) : 
  dim(X) must have a positive length
Can someone help me solve this please as am very new to R. Many thanks.
 
     
     
    