I have data similar to the following:
a<-data.frame(pig=c(NA,"a","b","c",NA),cow=c(NA,"101","112","77",NA),chicken=c("Sep",NA,"Oct","Nov",NA),stringsAsFactors=FALSE)
print(a)
pig cow chicken
<NA> <NA> Sep
a 101 <NA>
b 112 Oct
c 77 Nov
<NA> <NA> <NA>
I am attempting to paste the columns together and obtain NA if any NA is present in any of the paste columns for a given observation such as:
pig cow chicken rooster
<NA> <NA> Sep <NA>
a 101 <NA> <NA>
b 112 Oct b-112-Oct
c 77 Nov c-77-Nov
<NA> <NA> <NA> <NA>
I have used the following
a$rooster<-paste(a$pig,a$cow,a$chicken,sep="-")
This yields strings that include NAs as part of the string, which is not ideal. The documentation I have seen does not explicitly address this issue, such as: suppress NAs in paste() Any thoughts? Thanks!