Monday, December 19, 2022

[FIXED] How can I have R skip over one segment embedded in a wider line of code?

Issue

How can I have R skip over one segment embedded in a wider line of code? In the code below, I would like for R to compute the operations for var1 and var3.

df <- df %>% 
  mutate(newvar= df$var1 + df$var2 + df$var3)

To note: I get I could use # to have R ignore all the line, but I just want it to ignore + df$var2

Thank you,


Solution

Format the line differently and commenting works:

df <- df %>% 
  mutate(newvar= 
           df$var1 
         + df$var2 
         + df$var3
        )

Now you can comment out any of the parts, e.g.

df <- df %>% 
  mutate(newvar= 
           df$var1 
#        + df$var2 
         + df$var3
        )

Normally it's a bad idea to put operators at the start of a line instead of the end because R might think the previous line ends the whole statement, but in this the parens around the whole thing mean that's not an issue.



Answered By - user2554330
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.