I am using gsub to remove numbers for each element of a list. Code is given below. testList <- list("this contains a number 1000","this does not contain") removeNumbers <- function(X) { gsub("\\d","",X) } outputList <- lapply(testList,removeNumbers) However, when I try to find the number of words in outputList as follows outLength <- lapply(strsplit(outputList," "),length) it throws out the following error: Error in strsplit(outputList, " ") : non-character argument Can someone help me with this? Thank you. Ravi -- View this message in context: http://r.789695.n4.nabble.com/Removing-numbers-from-a-list-tp4023074p4023074.html Sent from the R help mailing list archive at Nabble.com.
This is probably what you want:> outLength <- lapply(outputList, function(x)length(strsplit(x, ' ')[[1]])) > outLength[[1]] [1] 4 [[2]] [1] 4>notice that 'outputList' is a 'list' and not a legal argument to strsplit. On Thu, Nov 10, 2011 at 5:29 AM, vioravis <vioravis at gmail.com> wrote:> I am using gsub to remove numbers for each element of a list. Code is given > below. > > ?testList <- list("this contains a number 1000","this does not contain") > ?removeNumbers <- function(X) > ?{ > ? ?gsub("\\d","",X) > ?} > ?outputList <- lapply(testList,removeNumbers) > > However, when I try to find the number of words in outputList as follows > > ?outLength <- lapply(strsplit(outputList," "),length) > > it throws out the following error: > > ?Error in strsplit(outputList, " ") : non-character argument > > > Can someone help me with this? > > Thank you. > > Ravi > > -- > View this message in context: http://r.789695.n4.nabble.com/Removing-numbers-from-a-list-tp4023074p4023074.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Nov 10, 2011, at 5:29 AM, vioravis wrote:> I am using gsub to remove numbers for each element of a list. Code > is given > below. > > testList <- list("this contains a number 1000","this does not > contain") > removeNumbers <- function(X) > { > gsub("\\d","",X) > } > outputList <- lapply(testList,removeNumbers) > > However, when I try to find the number of words in outputList as > follows > > outLength <- lapply(strsplit(outputList," "),length)By changing a single letter "l" -> "s" you could get that to succeed: > outputList <- sapply(testList,removeNumbers) > > outLength <- lapply(strsplit(outputList," "),length) > outLength [[1]] [1] 4 [[2]] [1] 4> > it throws out the following error: > > Error in strsplit(outputList, " ") : non-character argument >David Winsemius, MD West Hartford, CT