I am a beginner in R and wanted to know if there is any way to convert multiple vectors/variables into a desired 'class' (e.g. 3 variables within a dataset are factors, and I want to convert these 3 into numerical variables in one go).
Below is the dataset which contains columns "Product" as chr and the remaining columns as factors, however I want to keep "Product" and "Month" as character and "Sales" and "Profit" as numeric.
str(Conditional_function_IVY)
'data.frame': 100 obs. of 4 variables:
$ Product: chr "Bellen" "Bellen" "Sunshine" "Sunset" ...
$ Month : Factor w/ 12 levels "April","August",..: 5 5 5 5 5 5 5 5 4 4 ...
$ Sales : Factor w/ 88 levels " ? 501.00 "," ? 504.00 ",..: 8 13 64 16 55 78 81 29 2 52 ...
$ Profit : Factor w/ 65 levels " ? 100.00 "," ? 101.00 ",..: 44 34 5 15 39 16 37 38 65 56 ...
I've done it in the following way but it consumes a lot of time, hence I am wondering if there is any way which would let me do this in one go.
Conditional_function_IVY$Month=as.character(Conditional_function_IVY$Month)
> Conditional_function_IVY$Sales=as.numeric(Conditional_function_IVY$Sales)
> Conditional_function_IVY$Profit=as.numeric(Conditional_function_IVY$Profit)
> str(Conditional_function_IVY)
'data.frame': 100 obs. of 4 variables:
$ Product: chr "Bellen" "Bellen" "Sunshine" "Sunset" ...
$ Month : chr "January" "January" "January" "January" ...
$ Sales : num 8 13 64 16 55 78 81 29 2 52 ...
$ Profit : num 44 34 5 15 39 16 37 38 65 56 ...