Scott Chamberlain
2010-Dec-14 13:09 UTC
[R] Selecting non-empty elements after strsplit of string
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]]
Scott Chamberlain
2010-Dec-14 13:32 UTC
[R] Selecting non-empty elements after strsplit of string
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]]
Jinyan Huang
2010-Dec-14 16:06 UTC
[R] Selecting non-empty elements after strsplit of string
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. >