Home > Back-end >  If && condition to update a column in R
If && condition to update a column in R

Time:09-11

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}

CodePudding user response:

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.

  •  Tags:  
  • r
  • Related