lundi 27 février 2017

compare two integer column values in R and fill a new column

I am trying to fill new column values in R data frame based on a condition that compares values from two columns. Using for loop and if-else control statement.

Here's my sample dataset

Year1 | Year2 
----- | -----
1990  | 1990
1992  | 1992
1995  | 1998
1997  | 2000

I would like to do something like this:

for (i in 1:length(year1)
{
if (year 1 == year 2) 
   flag = 1 
else 
   flag = 2
}

This doesn't seem to be working. For some reason, all the conditions are evaluated as TRUE and flag is always 1.

Any suggestions would be much appreciated!

Aucun commentaire:

Enregistrer un commentaire