Using Filter in tm_map(testfile, removeNumbers) in R?

r, tm

Solution

`removeNumbers` will remove any digit. You can get its code like this:

getS3method("removeNumbers","PlainTextDocument")
function (x) 
gsub("[[:digit:]]+", "", x)

You should create a new function that remove "alone" digits , or digits after spaces.

remove_alone_nbr <- 
function (x) 
  gsub('\\s*(?<!\\B|-)\\d+(?!\\B|-)\\s*', "", x,perl=TRUE)

Then if you test it :

inspect(tm_map(Corpus(VectorSource(test.txt)), remove_alone_nbr))

You get :

this is a test file with numbers,and.
              The internet protocals ipv4 and ipv6

Problem

I am using tm_map(testfile, removeNumbers) to remove the numbers of a textfile. However, I need to retain the numbers that comes along with the words such as ipv4 and ipv6. How can I use the removeNumbers function to remove other numbers but keep the numbers that comes with ipv4 and ipv6.? This is the code I used: ``` test.txt = "this is a test file with numbers 1,2 and 3. The internet protocals ipv4 and ipv6" library(tm) test <- Corpus(DirSource('C:test'), readerControl = list(reader = readPlain)) test <- tm_map(test, removeNumbers) inspect(test[1]) ``` Output: ``` $test.txt this is a test file with numbers , and . The internet protocals ipv and ipv ```

Original source