Home > Mobile >  compose several values ​into one value in R
compose several values ​into one value in R

Time:10-07

I have a dataset for survived people on Titanic. Then I have tried to check if there is a correlation between survivors and the number of siblings on board. But there are too low data number on siblings over 2 so I will in my test in stead make two variable 0 for no siblings and 1 for at least 1 sibling. But how can I do this in R?

enter image description here enter image description here

CodePudding user response:

In the absence of any dputdata let me use some toy data to illustrate the solution:

titanic <- data.frame(
  randomVar = c(LETTERS[1:10]),
  siblings = c(1,0,2,3,5,1,0,2,0,1)
)

If you want to replace the count data in the column siblingswith binary values, i.e., 0 and 1, you can do this using an ifelse statement:

titanic$sibl_binary <- ifelse(titanic$siblings == 0, # condition to meet
                          0,                         # what to do if the condition is met
                          1)                         # what to do if the condition is not met

The result:

titanic
   randomVar siblings sibl_binary
1          A        1           1
2          B        0           0
3          C        2           1
4          D        3           1
5          E        5           1
6          F        1           1
7          G        0           0
8          H        2           1
9          I        0           0
10         J        1           1
  • Related