1

I am using below code to count occurrence of a word in a given sentence

wordCount = function(sentence,word){
splitedVectorString <- c() 
splitedVectorString <- strsplit(sentence," ")
count <- 0

for (j in splitedVectorString) {
  print(length(splitedVectorString))
  print(splitedVectorString)
  print(word)
  if (identical(word,j)) {
    count <- count + 1
    print(count)
  }
}
}

Program runs successfully but I'm getting count as 0. I call this function on console as

wordCount("This is XYZ and is running late","is")

When I print length of splited vector splitedVectorString it gives me 1. Am I getting issue in spliting the sentence ? Exactly I dont know whats going wrong. I just started learning R programming

Sotos
  • 51,121
  • 6
  • 32
  • 66
unflagged.destination
  • 1,576
  • 3
  • 19
  • 38
  • 4
    You might want to read [this question](http://stackoverflow.com/questions/7782113/counting-word-occurrences-in-r), it's almost the same (it's with a vector, but using `strsplit` you could transform your sentence in a vector). – etienne Dec 21 '16 at 10:16
  • Tried with length(grep(word,sentence)) but still getting 1 as output. I checked the length of splatted vector its giving me 1. Why the splatted vector "splitedVectorString" giving length as 1. Hence its not iterating throughout the vector and for loop just executing onnce – unflagged.destination Dec 21 '16 at 10:25
  • Use `length(grep("\\", strsplit("This is XYZ and is running late", " ")[[1]]))` – etienne Dec 21 '16 at 10:25
  • Or something like `sum(sapply(strsplit(sentence," ")[[1]], identical, y = word))` (closer to your function) – etienne Dec 21 '16 at 10:27

1 Answers1

1

What you could do is the following:

wordCount = function(sentence,word){

  splitedVectorString <- unlist(strsplit(sentence," "))
  count <- sum(word == splitedVectorString)
  count

  }

You unlist the result of strsplit so that you have a vector (strsplit returns a list and that is the reason you get length 1!) with all the words in your sentence and then you sum all the values that are equal to your word.

The expression word == splitedVectorString will return a vector of the same length as splitedVectorString with True and False depending on whether the particular element of the vector is the same as the word.

> wordCount("This is XYZ and is running late","is")
[1] 2
User2321
  • 2,952
  • 23
  • 46