David B
David B

Reputation: 25

running if and else within a for loop across columns

I am attempting to have R read across columns by row and evaluate whether values from two adjacent cells are equal. If the values are equal, I want R to count this occurence in a new variable. Here is example data (df):

Var1 Var2 Var3
2 3 3
3 3 3
1 2 3
3 2 1

...and I want to get here:

Var1 Var2 Var3 NewVar
2 3 3 1
3 3 3 2
1 2 3 0
3 2 1 0

One example set of code I have tried out is the following:

df$NewVar <- 0

for (i in 1:2){
  if (df[i]==df[i+1]){
    df$NewVar <- df$NewVar + 1
  }
  else{
    df$NewVar <- df$NewVar
  }
}

This particular set of code just returns 0s in the NewVar variable.

Any sort of help would be much appreciated!

Upvotes: 1

Views: 37

Answers (2)

akrun
akrun

Reputation: 887048

We can use Reduce

df$NewVar <- Reduce(`+`, Map(`==`, df[-1], df[-ncol(df)]))

data

df <- structure(list(Var1 = c(2L, 3L, 1L, 3L), Var2 = c(3L, 3L, 2L, 
2L), Var3 = c(3L, 3L, 3L, 1L)), class = "data.frame", row.names = c(NA,-4L))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388907

Here's a vectorized solution using rowSums :

df$NewVar <- rowSums(df[-1] == df[-ncol(df)])
df

#  Var1 Var2 Var3 NewVar
#1    2    3    3      1
#2    3    3    3      2
#3    1    2    3      0
#4    3    2    1      0

data

df <- structure(list(Var1 = c(2L, 3L, 1L, 3L), Var2 = c(3L, 3L, 2L, 
2L), Var3 = c(3L, 3L, 3L, 1L)), class = "data.frame", row.names = c(NA,-4L))

Upvotes: 2

Related Questions