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

If && condition to update a column in R

I’m trying to execute a simple line of command using if && operators in R. However, I don’t see any output, and there is no error as well. Wondering what’s happening. Appreciate all inputs. Thanks.

lws_data[,"WSR"] <- 
  if(lws_data$Ref && lws_data$PS >=270)
{lws_data$WSR=1}

>Solution :

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

If you want to apply if else logic across an entire column of a data frame in R, then use the ifelse() function:

lws_data$WSR <- ifelse(lws_data$Ref && lws_data$PS >= 270, 1, lws_data$WSR)

The base R ifelse() function is vectorized, meaning that it seamlessly works across columns in its one line form.

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