Cannot make mutate () work on factors

I have a data.frame named kids
I try to convert reassign values for factors to "YES", "NO" code factors
I get this error

levels (kids$arac)
[1] "0" "1" "2" "3" "4" "6"
library(dplyr)
kids%%
 mutate(fct_recode(arac,
 "0"    = "No",
 "1"    = "No",
 "6"    = "No",
 "2"    = "YES",
 "3"    = "YES",
  "4"    = "YES")
                 )
count (arac)
#> Error in eval(expr, envir, enclos): object 'kids' not found
  count (arac)
#> Error in eval(expr, envir, enclos): object 'arac' not found

Thanks
Created on 2024-03-23 with reprex v2.1.0

I modified your code and invented a small data frame to test it. Does this version work for you? Notice I changed the operator after kids, the order of the arguments in fct_recode and I added a pipe before count().

set.seed(123)
kids <- data.frame(arac = sample(c("0","1","2","3","4","6"), 12, replace = TRUE), 
                   stringsAsFactors = TRUE)
levels (kids$arac)
#> [1] "0" "1" "2" "3" "4" "6"
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(forcats)
kids %>%
  mutate(fct_recode(arac,
                   "No" = "0",
                   "No" = "1",
                   "No" = "6",
                   "YES" = "2",
                   "YES" = "3",
                   "YES" = "4")
  ) |> 
count(arac)
#>   arac n
#> 1    0 1
#> 2    1 2
#> 3    2 3
#> 4    3 1
#> 5    4 1
#> 6    6 4

Created on 2024-03-23 with reprex v2.0.2

Hi,
Thanks for your help
I should change the pipe from |> to %>% to make the code work. I do not why..
However I get the same levels and not the "YES", "NO" merge

levels (kids$arac)
library(forcats)
kids %>%
    mutate (fct_recode (arac,
    "NO"= "0",
    "NO"= "1",
    "NO"= "6",
    "YES"="3",
    "YES"="4",
    "YES"="6")
) %>%
count (arac)
# A tibble: 6 × 2
  arac      n
  <fct> <int>
1 0        43
2 1        25
3 2        19
4 3        13
5 4         3
6 6         7

Created on 2024-03-24 with reprex v2.1.0

Sorry, I missed that. You have to assign the result of fct_recode to the arac column.

set.seed(123)
kids <- data.frame(arac = sample(c("0","1","2","3","4","6"), 12, replace = TRUE), 
                   stringsAsFactors = TRUE)
levels (kids$arac)
#> [1] "0" "1" "2" "3" "4" "6"
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(forcats)
kids %>%
  mutate(arac = fct_recode(arac,
                   "No" = "0",
                   "No" = "1",
                   "No" = "6",
                   "YES" = "2",
                   "YES" = "3",
                   "YES" = "4")
  ) |> 
count(arac)
#>   arac n
#> 1   No 7
#> 2  YES 5

Created on 2024-03-23 with reprex v2.0.2

Thanks. It works now!

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.