0

I have a sample dataset , which has missing values in it.I want to create a new column with a message of different combinations where it should tell which columns values are missing.

Example:

Dataset:

A B C D

1 2 4

4 4

4 1

3 2 3

The permutaions of the above data set is :

"a" ,"b","c","d" ,"a, b","a, c" ,"a, d" , "b, c","b, d","c, d" , "a, b, c","a, b, d","a, c, d","b, c, d","a, b, c, d"

Result:

A B C D Message

1 2 4 Column B is missing

4 4 column A and D is Missing

4 1 Column C and D is Missing

All column values are missing

3 2 3 Column B is Missing

Any suggestion would be really appreciated

Shree
  • 10,835
  • 1
  • 14
  • 36
syeda
  • 1
  • 2
  • I have answered this question bu you **need** to see and follow [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help others help you. – Shree Jul 27 '19 at 14:14
  • Please provide any relevant code on how you attempted to solve this and where you had problems . This will make it easier and faster for the community to help you – camba1 Jul 27 '19 at 14:27

1 Answers1

0

Here's a way using apply from base R -

set.seed(4)
df <- data.frame(matrix(sample(c(1:5, NA), 15, replace = T), ncol = 3))
names(df) <- LETTERS[1:3]

df$msg <- apply(df, 1, function(x) {
  if(anyNA(x)) {
    paste0(paste0(names(x)[which(is.na(x))], collapse = " "), " missing", collapse = "")
  } else {
    "No missing"
  }
})

df

  A  B  C             msg
1 4  2  5      No missing
2 1  5  2      No missing
3 2 NA  1       B missing
4 2 NA NA     B C missing
5 5  1  3      No missing
Shree
  • 10,835
  • 1
  • 14
  • 36