trim NA from concatenate result

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

trim NA from concatenate result

R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: trim NA from concatenate result

Rui Barradas
Hello,

Something like this?


df1 <- read.table(text = "
NA NA NA
NA NA Adenocarcinoma
NA Other NA")
df1

apply(df1, 1, function(x){
   if(all(is.na(x))){
     NA_character_
   }else{
     paste(x[!is.na(x)], collapse = "")
   }
})


And please configure your e-mail client not to post in HTML format.

Hope this helps,

Rui Barradas

Às 22:07 de 02/06/21, Kai Yang via R-help escreveu:

> Hi List,
> I use paste function to concatenate  3 character columns together.
> when I run table to see that, I found 3 categories. How can I write script to trim NA in 2nd and 3rd group and set the first one as NA?
> Thanks,
> Kai
> NA NA NA
> NA NA Adenocarcinoma
> NA Other NA
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: trim NA from concatenate result

R help mailing list-2
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.