'fct_collapse in R?

I have a factor that's words (instances of words that difference participants said). I want to collapse it so that there are the categories "that" (every instance of the word "that") and notThat (all other words combined into one category). Naturally there are a lot of other words, and I don't want to go through and type them all. I've tried using != in various places, but it won't work. Maybe I just have the syntax wrong?

Anyway, is there a way to do this? That is, collapse all words that aren't "that" into one group?



Solution 1:[1]

How about this:

library(forcats)
x <- c("that", "something", "else")
fct_collapse(x, that = c("that"), other_level="notThat")
#> [1] that    notThat notThat
#> Levels: that notThat

Created on 2022-02-15 by the reprex package (v2.0.1)


Edit to show in a data frame

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)
dat <- data.frame(
  gender = factor(c(1,0,1,1,1,0), labels=c("male", "female")), 
  age = round(runif(6, 18,85)), 
  word = c("that", "something", "altogether", "different", "entirely", "that"))
dat %>% 
  mutate(word_collapse = fct_collapse(word, that="that", other_level="notThat"))
#>   gender age       word word_collapse
#> 1 female  74       that          that
#> 2   male  72  something       notThat
#> 3 female  57 altogether       notThat
#> 4 female  44  different       notThat
#> 5 female  79   entirely       notThat
#> 6   male  81       that          that

Created on 2022-02-15 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1