0

I have a data frame df with 100 rows. I am trying to make it so that if certain column values in each row equal certain things, I change another column value.

So in this example, for each row in the data frame, if the "subject" column value is = s002 and Deviant = 101 and Correct = 222, I want the column of "Correct Final" to be changed to 1

for (i in 100) {
df$CorrectFinal<- if (df$subject == "s002" && df$Deviant == 101 &&
 df$Correct == 222) (df$CorrectFinal == 1) else (df$CorrectFinal == 0)}
SBL
  • 87
  • 10
  • 1
    You need a vectorized function. Use` ifelse()` rather than `if/else` then no need for the `for` loop. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input (with maybe just 5 rows) and desired output that can be used to test and verify possible solutions. – MrFlick Aug 05 '20 at 20:56

1 Answers1

0

No loop needed. You are confusing ==, logical equals, with =, assignment:

df$CorrectFinal<- ifelse(df$subject == "s002" & df$Deviant == 101 &
 df$Correct == 222, 1, 0)
dcarlson
  • 10,936
  • 2
  • 15
  • 18