问题
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 (year1[i] == year2[i])
flag = 1
next
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.
I wonder why all conditions are evaluated as TRUE? I verified that the loop iterates over all values of year1.
Any suggestions would be much appreciated!
回答1:
Perhaps:
df$flag <- 2
df[df$Year1==df$Year2,]$flag <- 1
回答2:
How about
df$flag <- with(df, abs((Year1 == Year2) - 2L))
df
# Year1 Year2 flag
# 1 1990 1990 1
# 2 1992 1992 1
# 3 1995 1998 2
# 4 1997 2000 2
回答3:
Something along the lines of:
df$flag=ifelse(df$year1==df$year2,1,2)
回答4:
Another solution using apply function
df = data.frame(Year1 = c(1990, 1992, 1995, 1997), Year2 = c(1990, 1992, 1998, 2000))
apply function:
df$flag = apply(df, 1, function(x){ ifelse(x[1] == x[2], 1, 2) })
or using variable names
df$flag apply(df, 1, function(x){ ifelse(x["Year1"] == x["Year2"], 1, 2) })
回答5:
Above all the solutions compare the data with respect to same row and output the flag.
Rather, I would like you to consider this query. Which, not only covers above condition but also checks a column value (here year1) with the rest of the records present in another column (year2).
df = data.frame(Year1 = c(1990, 1998, 1992, 1997), Year2 = c(1990, 1992, 1998, 2000))
df$flag <- ifelse(df$Year1 %in% df$Year2, 1, 2)
df
# Result:
# Year1 Year2 flag
#1 1990 1990 1
#2 1998 1992 1
#3 1992 1998 1
#4 1997 2000 2
Hope it helped!
来源:https://stackoverflow.com/questions/42497851/compare-two-integer-column-values-in-r-and-fill-a-new-column