creating new function to validate the data between two columns

In my data frame i have many columns which is dependent on first column. for example in question one i have to ask if a person have mobile the answer should be yes or no and in another question we are asking which model if they answer yes to first question.

so i want to create a new data frame which text for if a person answered first question in Yes or any then there should be text in second question which is dependent on first question. for example i have shown in picture

going forward i will create a summary tables with all the new columns.

df <- data.frame("have mobile" = c("Yes",	"No",	"No",	"No",	"No",	"Yes",	"No",	"Yes",	"Yes",	"No",	"No",	"No",	"Yes"),
                 "Which model" = c("c1",	NA,	NA,	NA,	NA,	"c2",	NA,	"c2",	"c3",	NA,	NA,	NA,	"c3"))


func1 <- function(df,col_1,col_var_1,col2,col_var2){
  
  df[new_col1] <- ifelse(df[col_1]==col_var_1,df[col2]==is.na(df[col2]),"should be ok","")
 
  df 
}

image

It's not a challenge for dplyr:

library(dplyr)

df <- data.frame("have mobile" = c("Yes",	"No",	"No",	"No",	"No",	"Yes",	"No",	"Yes",	"Yes",	"No",	"No",	"No",	"Yes"),
                 "Which model" = c("c1",	NA,	NA,	NA,	NA,	"c2",	NA,	"c2",	"c3",	NA,	NA,	NA,	"c3"))


df %>% mutate(validation = if_else(have.mobile=="Yes" & !is.na(Which.model),"Should be OK", ""))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.