Skip to content

Instantly share code, notes, and snippets.

@jilmun
Created March 8, 2016 14:59
Show Gist options
  • Save jilmun/1f3fc0956525a33ae37d to your computer and use it in GitHub Desktop.
Save jilmun/1f3fc0956525a33ae37d to your computer and use it in GitHub Desktop.
fn_NApattern <- function(myrow) {
return(paste(is.na(myrow)+0, collapse=""))
}
# test function with dummy data
(df_dummy <- data.frame(X1=c(5,NA,3,NA,3,2,5,2),
X2=c(NA,0,3,NA,3,1,NA,2),
X3=c(1,5,NA,NA,6,1,NA,2),
X4=c(NA,NA,3,3,3,1,NA,NA)))
# X1 X2 X3 X4
# 1 5 NA 1 NA
# 2 NA 0 5 NA
# 3 3 3 NA 3
# 4 NA NA NA 3
# 5 3 3 6 3
# 6 2 1 1 1
# 7 5 NA NA NA
# 8 2 2 2 NA
# one row test case
fn_NApattern(df_dummy[1,])
# [1] "0101"
# data frame test case
apply(df_dummy, 1, fn_NApattern)
# [1] "0101" "1001" "0010" "1110" "0000" "0000" "0111" "0001"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment