Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two true/false variables in R?

Tags:

r

I have four variables as below in a data.frame which continues on thousands of rows:

One      Two      Three    Four 

TRUE     TRUE     FALSE    FALSE
FALSE    TRUE     TRUE     TRUE
TRUE     FALSE    FALSE    TRUE
TRUE     TRUE     TRUE     FALSE
FALSE    TRUE     FALSE    TRUE
FALSE    FALSE    TRUE     FALSE
TRUE     FALSE    FALSE    TRUE

I want to create two new variables, one which merges columns one and two, the second which merges columns three and four. So each new column would display TRUE if either or both of the two columns displayed TRUE, and would display FALSE if both were false. The resulting data would look like this:

One      Two      OneTwo     Three    Four    ThreeFour

TRUE     TRUE     TRUE       FALSE    FALSE   FALSE
FALSE    TRUE     TRUE       TRUE     TRUE    TRUE
TRUE     FALSE    TRUE       FALSE    TRUE    TRUE
TRUE     TRUE     TRUE       FALSE    FALSE   FALSE
FALSE    FALSE    FALSE      FALSE    TRUE    TRUE
FALSE    FALSE    FALSE      TRUE     FALSE   TRUE
TRUE     FALSE    TRUE       FALSE    TRUE    TRUE

Any help would be much appreciated. I've looked through some other questions but can't find how to do this specifically.

like image 509
Amy Avatar asked Jul 18 '20 13:07

Amy


People also ask

How do I combine two values in R?

How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.

How do I merge two data frames in the same column in R?

The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.

How do I combine cases in R?

To merge two data frames (datasets) horizontally, use the merge() function in the R language. To bind or combine rows in R, use the rbind() function. The rbind() stands for row binding.

How do I join data in R?

We can merge two data frames in R by using the merge() function or by using family of join() function in dplyr package. The data frames must have same column names on which the merging happens. Merge() Function in R is similar to database join operation in SQL.


1 Answers

Using the package dplyr you can do this:

library(dplyr)
data <- data %>% mutate(
   OneTwo = as.logical(One + Two),
   ThreeFour = as.logical(Three + Four))

This works since TRUE and FALSE are actually saved as 1 and 0 by the computer. R then codes values larger 0 as TRUE. To be a bit more "correct", you could also use this code, to get back 0s and 1s before converting them to logicals:

library(dplyr)
data <- data %>%
   mutate(
    OneTwo = as.logical(pmax(One, Two)),
    ThreeFour = as.logical(pmax(One, Two)))
like image 88
elehna Avatar answered Sep 23 '22 13:09

elehna