[R] Selecting non-empty elements after strsplit of string

Jinyan Huang jinyan.fr at gmail.com
Tue Dec 14 17:06:00 CET 2010


unlist(strsplit(output," +"))


On Tue, Dec 14, 2010 at 2:09 PM, Scott Chamberlain
<myrmecocystus at gmail.com> wrote:
> Hello,
>
> I am attempting to manipulate strings in which there are differing amounts of whitespace before and after each element taht I want to keep (any word, letter, or number). However, after strsplit and unlist, I know how to select specific elements with "[ ]", but I want to select instead all elements that are not missing. That is, do not select any elements that are simply "".
>
> output <- c("a     b   8888      6")
>
> gsub(" ","",unlist(strsplit(output,"  ",fixed=TRUE)))
> [1] "a"    ""     "b"    "8888" ""     ""     "6"
>
> Thanks! Scott Chamberlain
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



More information about the R-help mailing list