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

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Dec 14 17:05:06 CET 2010


Hi,

I have just recently started to use regular expression, so I'm not sure 
that my solution would be valid in every case. But it works in yours:
 > unlist(strsplit(output, split=" +"))
[1] "a"    "b"    "8888" "6"

It would split them whatever the number of spaces between them is 
(because of the "+" in the pattern of splitting).

HTH,
Ivan

Le 12/14/2010 14:32, Scott Chamberlain a écrit :
> 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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php



More information about the R-help mailing list