Dealing with function creation

Hey there!

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.

categorical <- function(put){
  sueldo$put <- factor(sueldo$put, levels = c(1,2),
                       labels= c('sí','No'))
}

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
set.seed(42)
dummy <- data.frame(matrix(sample(1:2,25,replace = TRUE), ncol = 5))
str(dummy)
#> 'data.frame':    5 obs. of  5 variables:
#>  $ X1: int  1 1 1 1 2
#>  $ X2: int  2 2 2 1 2
#>  $ X3: int  1 2 1 2 1
#>  $ X4: int  1 2 2 2 2
#>  $ X5: int  1 1 1 1 1

make_cats <- function(x) {
  levels(x) <- c('sí','No')
  for(i in 1:length(x)) x[,i] = as.factor(x[,1])
}

make_cats(dummy)
str(dummy)
#> 'data.frame':    5 obs. of  5 variables:
#>  $ X1: int  1 1 1 1 2
#>  $ X2: int  2 2 2 1 2
#>  $ X3: int  1 2 1 2 1
#>  $ X4: int  1 2 2 2 2
#>  $ X5: int  1 1 1 1 1

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.