Suppress Nas in Paste()

suppress NAs in paste()

For the purpose of a "true-NA": Seems the most direct route is just to modify the value returned by paste2 to be NA when the value is ""

 paste3 <- function(...,sep=", ") {
L <- list(...)
L <- lapply(L,function(x) {x[is.na(x)] <- ""; x})
ret <-gsub(paste0("(^",sep,"|",sep,"$)"),"",
gsub(paste0(sep,sep),sep,
do.call(paste,c(L,list(sep=sep)))))
is.na(ret) <- ret==""
ret
}
val<- paste3(c("a","b", "c", NA), c("A","B", NA, NA))
val
#[1] "a, A" "b, B" "c" NA

How to skip a paste() argument when its value is NA in R

The alternative is to just fix it up afterwards:

gsub("NA, ","",dff$string)

#[1] "Austin, Texas, United States"
#[2] "Knoxville, Tennessee, United States"
#[3] "Salk Lake City, Utah, United States"
#[4] "Prague, Czech Rep"

Alternative #2, is to use apply once you have your data.frame called dff:

apply(dff, 1, function(x) paste(na.omit(x),collapse=", ") )

Paste columns together without NAs becoming characters

Paste columns using na.omit, see example:

# reproducible example
owners4 <- data.frame(FirstName = c("Aa", "Bb", NA),
MiddleInitial = c("T", "U", NA),
LastName = c(NA, "Yyy", NA))

owners4$Name <- apply(owners4[, c("FirstName", "MiddleInitial", "LastName")], 1,
function(i){ paste(na.omit(i), collapse = " ") })

owners4
# FirstName MiddleInitial LastName Name
# 1 Aa T <NA> Aa T
# 2 Bb U Yyy Bb U Yyy
# 3 <NA> <NA> <NA>

Now filter out rows where Name is blank

result <- owners4[ owners4$Name != "", ]
result
# FirstName MiddleInitial LastName Name
# 1 Aa T <NA> Aa T
# 2 Bb U Yyy Bb U Yyy

How to omit NA values while pasting numerous column values together?

You could try na.omit() to omit the values, then paste. Also, you could use toString(), as it is the equivalent of paste(..., collapse = ", ").

apply(dd2, 1, function(x) toString(na.omit(x)))
# [1] "A, AK2, PPT" "B, HFM1, PPT" "C, TRR"
# [4] "D, TRR, RTT, GGT" "E, RTT"

If you have specific columns you are using then

apply(dd2[, cols], 1, function(x) toString(na.omit(x)))

R Paste - ignore cases where at least one element I am pasting is NA

You can combine it with ifelse like this

ifelse(is.na(vector1), NA, paste("#", vector1, "something" ,sep = ""))

Although this was marked as answered please also see the answer of @hello_friend which may be more appropriate.

cleanly paste columns that contain NAs

Here's a way in base R -

dat$col3 <- apply(dat, 1, function(x) paste0(na.omit(x), collapse = "; "))

col1 col2 col3
1 stuff things stuff; things
2 stuff <NA> stuff
3 stuff things stuff; things
4 <NA> things things
5 <NA> <NA>

Omit NA values while pasting two column values together in R

You could do something like this, temporarily replacing NA values with the empty character "".

cbind(
dd2,
combination = paste(dd2[,2], replace(dd2[,3], is.na(dd2[,3]), ""), sep = "*")
)
# customer_sample_id Left.Gene.Symbols Right.Gene.Symbols combinations
# [1,] "AMLM12001KP" "AK2" NA "AK2*"
# [2,] "AMLM12001KP" "HFM1" "PPT" "HFM1*PPT"
# [3,] "AMLM12001KP" "HFM1" NA "HFM1*"
# [4,] "AMLM12001KP" "HFM1" "GGT" "HFM1*GGT"
# [5,] "AMLM12001KP" "HFM1" NA "HFM1*"

Of course substitute your column names for the column numbers above. I didn't write them because they are too long.

Paste together strings which come from names that fit a pattern and ignore NAs

We can use unite with na.rm

library(dplyr)
library(tidyr)
df %>%
unite(included, starts_with('include'), na.rm = TRUE, sep = "| ") %>%
select(ignore, included)

-output

# A tibble: 3 x 2
# ignore included
# <chr> <chr>
#1 j a| d| f| i
#2 k b| e| g
#3 l c| h


Related Topics



Leave a reply



Submit