1

I am having trouble finding a solution to this: I want to replace those NA values with some text depending on the name of the variable. Ideally it would do something like:

if variable name is var2 or var3 or var4 replace in the same row with "text"

               variable question
1               var1     <NA>
2               var2     <NA>
3               var3     <NA>
4               var4     <NA>

The closest I thought would work was:

df$question[df$variable = var2 OR var3 OR var4] <- text

it shouldn't be hard, i am just to blind to find the right answers :(.

1
  • 1
    It seems like just a syntax issue, you had the right idea. If you use the following conditional statement, it'll work: (df$variable == "var2"| df$variable == "var3" | df$variable == "var4"). Note the use of | (elementwise comparison) Commented Nov 24, 2013 at 14:57

1 Answer 1

4

If text is the same for var2, var3, var4 then, this should do the trick. Otherwise, if text is different for each var*, then update your question and provide us with more details.

> df$question <- as.character(df$question)
> df$question[df$variable %in% c("var2", "var3", "var4")] <- "text"
> df
  variable question
1     var1     <NA>
2     var2     text
3     var3     text
4     var4     text
Sign up to request clarification or add additional context in comments.

1 Comment

worked perfectly, thank you! It really was simple as that, similar text for all matching variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.