Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading