0
votes

I am using the Corpus function to read a file I have created in the below mentioned directory.

chk <- Corpus(DirSource("C:\\Users\\TCS Profile\\Documents\\R\\Machine Learning Text\\Naive Bayes"))

Once the corpus is created, when i validate the variable chk created, i find that the content has been read:

 str(chk)
List of 1
 $ Test.txt:List of 2
  ..$ content: chr [1:7] "Hi Wassup" "How are You" "Hope it Works!!!" "" ...
  ..$ meta   :List of 7
  .. ..$ author       : chr(0) 
  .. ..$ datetimestamp: POSIXlt[1:1], format: "2015-10-14 16:15:17"
  .. ..$ description  : chr(0) 
  .. ..$ heading      : chr(0) 
  .. ..$ id           : chr "Test.txt"
  .. ..$ language     : chr "en"
  .. ..$ origin       : chr(0) 
  .. ..- attr(*, "class")= chr "TextDocumentMeta"
  ..- attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"
 - attr(*, "class")= chr [1:2] "VCorpus" "Corpus"

The problem is I am unable to access a specific value within the content, let's say the 3rd element. (Hope it Works!!) I have tried using the following code:

chk[[1]][1,3]

Error in chk[[1]][1, 3] : incorrect number of dimensions

Can anyone please tell me how can I access the corresponding element and why such an error is coming for the above type of access?

1
chk$Test.txt$content[3] is the clearest way. Modifying your way: chk[[1]][[1]][3]. Run it in parts so you can figure out how to do it next time, e.g., chk$Test.txt and chk$Test.txt$contentFrank
@ Frank- I had tried that option as well:Dilip T Monson
Well, it should work. Without seeing your actual example, no one but you can figure out why it doesn't.Frank
The option chk [[1]][[1]][3] works. Thanks a lot. However, when I try Chk$Test.txt I get Null. Any idea why it works like thatDilip T Monson
It still shows NULL. Might be some other issue I guess, Thanks for the input. In case, I am able to find the answer, will let you know... Really appreciate the help... :)Dilip T Monson

1 Answers

1
votes

This should work:

> chk[[1]][1]$content[3]
#[1] "Hope it Works!!!"

I used this data to reproduce your example:

chk <-structure(list(content = list(structure(list(content =    c("Hi Wassup ", "How are You ", "Hope it Works!!!", "", "long time no see ", "Howdy", "Yo"), 
meta = structure(list(author = character(0),  datetimestamp = structure(list(sec = 12.238600730896, min = 17L, hour = 19L, mday = 14L, mon = 9L, year = 115L, wday = 3L, yday = 286L, isdst = 0L), 
.Names = c("sec", "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"), 
class = c("POSIXlt", "POSIXt"), tzone = "GMT"), description = character(0), heading = character(0), id = "Test.txt", language = "en", 
origin = character(0)), .Names = c("author", "datetimestamp", "description", "heading", "id", "language", "origin"), 
class = "TextDocumentMeta")), .Names = c("content", "meta"), class = c("PlainTextDocument", "TextDocument"))), meta = structure(list(), class = "CorpusMeta"), 
dmeta = structure(list(), .Names = character(0), row.names = 1L, class = "data.frame")), 
.Names = c("content", "meta", "dmeta"), class = c("VCorpus", "Corpus"))