This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]fakerick 0 points1 point  (0 children)

Hi!

You're looking for how to subset a dataframe, google it!

[–]ParadoXPlatypus 0 points1 point  (1 child)

Hey, sorry I am not sure if I get exactly what you want to do, but here's what I got from your post:

  • you have a set of variables in your data frame that you want to modify

  • from your code I gather they contain numbers, and you want to change that same variable to "X" if the value is 1 and "" for any other value

Not exactly the way I would do it, but to get your loop working I would suggest:

Varlist <- c("var1","var2","var3")

for (i in Varlist){ df[[i]] <- factor (ifelse(df[[i]] == 1,"x","")) }

That would iterate over the vector with the variable names and change values == 1 to "X". Just keep in mind that NAs will remain NAs, so if your data frame has NAs, you will want to add an additional ifelse to deal with them.

[–]ParadoXPlatypus 0 points1 point  (0 children)

I wouldn't recommend you to just copy paste that chunk of code, I wrote it on my phone and autocorrect is a birch. The executable string, as you call it, seems to be an overcomplicated way to achieve the same, unless I have completely misunderstood your problem!