antonina
antonina

Reputation: 109

If Loop with several conditions

I have the following dataframe:

df1
Name      Ch1      Val1
A          a         x1
B          b         x2
C          a         x3
...

And I want to add another row that gives me a solution on the loop I am trying to get:

for (i in nrow(df))
  if ( (df[i,3]>=-2)==T & (df3[i,3] <=2)==T & df[i,2]=="a"){
    df[i,4]<-TRUE
  }else if ((df[i,3]>2)==T & df[i,2]=="b"){
    df[i,4]<-TRUE
  }else (df[i,4]<-FALSE)

So basically if the value in Val1 is in an interval of -2 and +2 AND Ch1 is "a" it should result in TRUE OR if Val1 is bigger than 2 AND Ch1 is "b" then the result is TRUE

Otherwise it should always be false.

My loop seems to only return the result for the first row the rest is NA. Any idea where the mistake is? Or another way to solve this (even though I actually have a few more ORs)

Thank you!

Upvotes: 0

Views: 56

Answers (2)

Carsten Stann
Carsten Stann

Reputation: 44

Your for loop only does one iteration because it is passed a single value instead of a sequence: i takes on only the single value you specify, not each value in a sequence such as each number from 1 up to nrow(df).

For example:

df <- data.frame(a = 1:5)
for (i in nrow(df)) {
print(i)
}

results in: 5

but,

for (i in 1:nrow(df)) {
print(i)
}

results in: 1 2 3 4 5

but the answer posted by @annet is more elegant.

Upvotes: 1

Annet
Annet

Reputation: 866

If I understand correctly you try to create a new column, which contains true or false. I would use dplyrfor this.

df <- df %>% 
   mutate(new_column = case_when(
 Val1 >=-2  & Val1 <=2 & Ch1 =="a" ~ TRUE,
Val1 > 2 & Ch1 == "b" ~ TRUE,
TRUE ~ FALSE
))

Upvotes: 2

Related Questions