vendredi 18 décembre 2020

Looping over a column and replacing values if condition is met in r

I'm still a beginner to R, and I'm trying to figure out how to rewrite my indexing in my loop.

I have a column party that just lists political party affiliations. I have a loop that I wrote to replace any values that are not either democrat or republican in the column.

for(i in 1:length(party3$party)){
  if(party3$party[i] != "democrat" || party3$party[i] != "republican"){
  party3$party[i] <- "I" }}

My column party looks something like this:

party
republican
independent
democrat
libertarian
independent
american independent
republican
democrat

And I'm trying to keep democrat and republican intact:

party
republican
I
democrat
I
I
I
republican
democrat

Problem is, everything ends up being replaced with "I". How do I fix this loop's indexing?

Aucun commentaire:

Enregistrer un commentaire