In my data frame i have more than 1000 columns and I am validating duplicate entries in data frame.
so in actual I have columns from ID1- ID 1000 and I want to check if there is any duplicate entry in any row from ID1-ID100 then I want to mutate new column with text "duplicate entry" and want to add that column before "Name" column. i am trying to create a function for this.
Below dataframe is sample dataframe
df <- data.frame(ID =c("DEV2962","KTN2252","ANA2719","ITI2624","DEV2698","HRT2921",NA,"KTN2624","ANA2548","ITI2535","DEV2732","HRT2837","ERV2951","KTN2542","ANA2813","ITI2210"),
city=c("DEL","mum","DEL","MUM","DEL","del","MUM","DEL","del","MUM","mum","mum","mum","mum","DEL","DEL"),
Name= c("dev,akash","singh,Ajay","abbas,salman","lal,ram","singh,nkunj","garg,prabal","ali,sanu","singh,kunal","tomar,lakhan","thakur,praveen","ali,sarman","khan,zuber","singh,giriraj","sharma,lokesh","pawar,pooja","sharma,nikita"),
ID2 = c("A0011","A0011","A0011","A0011","A0011","A0012","AB702","AB328","AC728","AC314","AC742","AC919","AC062","AD712","AD021","AD920"),
ID3 = c("A0012","A0012","A0012","A0012","A0012","A0013","AB712","AB712","AB702","AB328","AC314","AC728","AB702","AB712","AC742","AC919"),
ID4 = c(NA,"A0013","A0013","A0013","AB982","AB982",NA,"AB982","A0013","A0012","A0012","A0012","A0012",NA,"A0013","A0012"),
ID5 =c(NA,"A0012","AB012","AB012",NA,"AB702",NA,"A0013",NA,"A0011","A0011",NA,"A0011",NA,NA,NA),
ID6 = c(NA,NA,NA,"AB982",NA,NA,NA,"A0012",NA,NA,NA,NA,NA,NA,NA,NA),
ID7 = c(NA,NA,NA,NA,NA,NA,NA,"A0011",NA,NA,NA,NA,NA,NA,NA,NA),
ID8 =c(NA,NA,NA,NA,NA,NA,NA,"AB982",NA,NA,NA,NA,NA,NA,NA,NA))
hasDups <- function(df){
checkDup <- function(x){
ifelse(max(table(x)) > 1, "duplicate available", "")
}
df$hasDup <- apply(df, 1, checkDup)
col_nams <- c(c("ID", "city", "Name", "hasDup"),
setdiff(names(df), c("ID", "city", "Name", "hasDup")))
df <- df[, col_nams]
return(df)
}
hasDups(df)