I have a data frame with several categorical variables. I want some of those variables to be changed as factors in R, the have the same components since it was a survey (yes/no answers)/. I want to create a function to just insert all these variables and automatically be changed to factor insted of copying and pasting on and on.
However, when I insert my variable I get the error:
Error: Assigned data factor(sueldo$put, levels = c(1, 2), labels = c("sí", "No")) must be compatible with existing data. x Existing data has 28394 rows. x Assigned data has 0 rows. i Only vectors of size 1 are recycled.
This is for an institutional project thanks for your help guys.
In this context, it is easier to use the [[ operator to select a data frame column than the $ operator.
categorical <- function(put){
+ factor(sueldo[[put]], levels = c(1,2),
+ labels= c('sí','No'))
+ }
> sueldo <- data.frame(Fac = c(2,1,1,2), Value = 1:4)
> categorical("Fac")
[1] No sí sí No
Levels: sí No
> sueldo #sueldo is unchanged
Fac Value
1 2 1
2 1 2
3 1 3
4 2 4
> sueldo$Fac <- categorical("Fac")
> sueldo
Fac Value
1 No 1
2 sí 2
3 sí 3
4 No 4