Search code examples
rfactors

Adding a factor from another factor


I have 5 dichotomous variables (0=no and 1=yes). Selecting 'yes' indicates that a subject stated this behavior is a part of their coping skills (e.g., meditation; going for a walk). I have summed the coping skills variable and then created a factor (0 skills used; 1-2 skills used; 3 or more skills used).

levels(x$var1)
[1] "0"  "1"

Total coping skills factor variable:

x %>% count(cop.skills)
# A tibble: 3 x 2
  str.12     n
  <fct>  <int>
1 0       3743
2 1-2     4398
3 3+      2632

There's a sixth dichotomous variable asking, "I have never done any of these behaviors (listed above)," with a dichotomous answer (0=no and 1=yes).

 x %>% count(var6)
# A tibble: 2 x 2
  stress2     n
  <fct>   <int>
1 0       11456
2 1          77

I would like to add the "yeses" to the "0" level of cop.skills variable. "cop.skills" would look like this:

x %>% count(cop.skills)
# A tibble: 3 x 2
  str.12     n
  <fct>  <int>
1 0       3820
2 1-2     4398
3 3+      2632

Not sure how to do this.


Solution

  • Why not just add them to your first result?

    result    <- x %>% count(cop.skills)
    no_skills <- x %>% count(var6)
    result[1, 2] <- result[1, 2] + no_skills[2, 2]