Search code examples
rvalidationlogical-operatorsisnullorempty

R: Validate Data for Numeric and Non-Numeric Variables


I got problem with R that I cant figure it out.

Here's my dataframe "test1" dataframe1

I would like to validate if the data follows the rules (Rule1: if Q1=1, Q2=Blank Rule2: if Q1!=1, Q2!=Blank) so that I can have the results as below results

However, I got the warning error "the condition has length > 1 and only the first element will be used".

Can anyone helps me with this?

Thanks a lot.


Solution

  • Based on the rules

    df1$Result <- with(df1, (Q1==1 & Q2 == "")|(Q1 !=1 & Q2 !=""))
    

    data

    df1 <- data.frame(i..key = 1:9, Q1 = c(1:3, 1, 2, 2, 3, 1, 3), 
      Q2 = c("", "abc", "fgdgg", "", "fdg", "", "dsfdsfds", "dfds", ""), stringsAsFactors=FALSE)