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.
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>.
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.
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.
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.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With