user2897366
user2897366

Reputation:

counting how often a given word occurs in a given file, ignoring case using R

for example :

currency <- grepl ("currencry" , strsplit("euro currency is a convertible currency"," "), ignore.case=TRUE) 

but it returns only TRUE how can I count how many times the word currency is appeared in this sentence using this grepl function ?

Sorry but I am a beginner . Thanks in advance

Upvotes: 3

Views: 213

Answers (5)

Rich Scriven
Rich Scriven

Reputation: 99351

You can also use table

> s <- strsplit("euro currency is a convertible currency", " ")[[1]]
> tab <-table(s)
> tab["currency"]
# currency 
#        2 

This is nice is you need to store the table of all words. You can look up any word with tab["word"] For example

> tab["convertible"]
# convertible
#           1

Upvotes: 0

Tyler Rinker
Tyler Rinker

Reputation: 109984

You could use the qdap package:

library(qdap)

termco("euro currency is a convertible currency",, "currency")

##   all word.count  currency
## 1 all          6 2(33.33%)

Upvotes: 0

agstudy
agstudy

Reputation: 121598

No need to use regular expression here:

 sum(scan(text="euro currency is a convertible currency",
          what="character") == "currency")
 ## 2

In case you want to ignore case -)

 sum(scan(text=tolower("euro curreNcy is a convertible currencY"),
          what="character") == "currency")

Upvotes: 5

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

I would use gregexpr as follows:

lapply(gregexpr("currency", 
                "euro currency is a convertible currency", 
                ignore.case=TRUE), 
       length)
# [[1]]
# [1] 2

(And I would make sure that you are spelling your search pattern correctly).

Upvotes: 3

plannapus
plannapus

Reputation: 18759

strsplit returns a list (so that you can input a vector of character). You need to "unlist" it first:

currency <- grepl ("currency" , unlist(strsplit("euro currency is a convertible currency"," ")), ignore.case=TRUE)
currency
[1] FALSE  TRUE FALSE FALSE FALSE  TRUE
sum(currency)
[1] 2

Upvotes: 4

Related Questions