Home > Back-end >  Combining columns in R dataframe
Combining columns in R dataframe

Time:03-25

I'm new to R and have a rather simple question.

I have a dataframe based on a survey. However, my variables are kind of deconstructed, meaning that for example my variable for gender is split into two columns (one for man and one for woman). In the screenshot you can see, what I'm talking about. In the variable for men (1 = man and everything else equals 0. The same is the case for the variable for women).

My question is how I create a new column where the two is combined and man = 1 and woman = 2). All my variables are deconstructed this way, so I will also need to combine columns for variables with more levels, such as education. I hope you can help :) Thanks!

I tried searching online but haven't found answers

CodePudding user response:

As @Allan Cameron said in the comments, you could just multiply resp_mand by 2 and sum columns to create a column gender. You can use the following code:

df <- data.frame(resp_kvinde = c(1,1,1,0),
                 resp_mand = c(0,0,0,1))

  resp_kvinde resp_mand
1           1         0
2           1         0
3           1         0
4           0         1

library(dplyr)
df %>%
  mutate(gender = resp_kvinde   2*resp_mand)

Output:

  resp_kvinde resp_mand gender
1           1         0      1
2           1         0      1
3           1         0      1
4           0         1      2

CodePudding user response:

Here is a tidyverse solution following what Allan Cameron mentioned. If you work in R it might be more suitable to turn it into a factor (as shown below) to ensure valid data types for further statistical analysis:

library(tidyverse)

data <- tribble(
~ resp_kvinde, ~resp_mand,
1, 0,
1, 0,
1, 0,
0, 1,
0, 1
)

data <-
  data %>%
  mutate(gender = resp_kvinde   2 * resp_mand,
         gender2 = fct_recode(as_factor(gender),
                              "man" = "1",
                              "woman" = "2")
  )

data
#> # A tibble: 5 × 4
#>   resp_kvinde resp_mand gender gender2
#>         <dbl>     <dbl>  <dbl> <fct>  
#> 1           1         0      1 man    
#> 2           1         0      1 man    
#> 3           1         0      1 man    
#> 4           0         1      2 woman  
#> 5           0         1      2 woman
  • Related