1
t1 <- c('064359208644', '108595042227', '115705579765', '139254098290', '163646243244')
t2 <- c('064359208644', '139254098290', '163646243244')

missing_elem <- as.character(setdiff(t1, t2))
missing_elem

if(length(missing_elem) != 0){
  ss <- cat("Missing ids:", missing_elem)
}else{
  ss <- "There are no missing ids"
}

ss

When I print the output of ss here it shows NULL but if t1 and t2 are the same and length(missing_elem) is 0 then ss is properly set to "There are no missing ids" and shows accordingly. Any idea why this might happen?

0

2 Answers 2

2

The function cat does not return concatenated strings, but rather prints to standard output and returns NULL (which is why the value of ss is NULL). You likely want to use paste:

if(length(missing_elem) != 0){
  ss <- paste("Missing ids:", paste(missing_elem, collapse = ', '))
}else{
  ss <- "There are no missing ids"
}

> ss
[1] "Missing ids: 108595042227, 115705579765"
Sign up to request clarification or add additional context in comments.

1 Comment

AAhhh!! Good catch. Thank you.
2

If you want to just get a character back, just use paste instead of cat.

1 Comment

Fwiw, in my own code I do cat("Missing IDs:", paste("\t", missing_elem), sep="\n") for this. You could add a quote from ?cat, like "Value: None (invisible NULL)."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.