0

What I want to do is generate a new column in a dataframe that meets these conditions:

dataframe1$var1 == dataframe2$var1 &
dataframe1$var2 == dataframe2$var2 &
dataframe1var3 == dataframe3$var3*

Basically I need to generate a dummy variable that has the value 1 if the conditions are met, and the value 0 if they are not.

I've tried the following code that doesn't work:

dataframe1$NewVar <- ifelse(dataframe1$var1 == dataframe2$var1 & 
dataframe1$var2 == dataframe2$var2 & dataframe1$var3 == dataframe2$var3 , 1, 0)

Data

dput(df1)
structure(list(var1 = c("A", "B", "C"), var2 = c("X", "X", "X"
), var3 = c(1, 2, 2)), .Names = c("var1", "var2", "var3"), row.names = c(NA, 
-3L), class = "data.frame")

dput(df2)
structure(list(var1 = c("A", "A", "C"), var2 = c("X", "X", "Y"
), var3 = c(1, 1, 1)), .Names = c("var1", "var2", "var3"), row.names = c(NA, 
-3L), class = "data.frame")
Sotos
  • 51,121
  • 6
  • 32
  • 66
Louise Sørensen
  • 225
  • 1
  • 11
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Dec 15 '17 at 10:08

2 Answers2

0

btw my dataset is not as simple as the example I posted in the pictures.

I don't know if it's relevant but values in my variables (columns) would look like this:

var1: 24000000000 var2: 1234567 var3: 8

Louise Sørensen
  • 225
  • 1
  • 11
  • 2
    Please edit your question to add any additional info. Don't use the "answer" section unless you are going to actually answer the question – Sotos Dec 15 '17 at 10:08
0

You can simply do,

as.integer(rowSums(df1 == df2) == ncol(df1))
#[1] 1 0 0
Sotos
  • 51,121
  • 6
  • 32
  • 66