[R] Removing numbers from a list

David Winsemius dwinsemius at comcast.net
Thu Nov 10 15:23:11 CET 2011


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



More information about the R-help mailing list