arcee123
arcee123

Reputation: 211

how to count words in a data frame based on a list of dictionary terms in r

I'm trying to perform a dictionary count of terms in a factor of strings.

I have a factor called Names. An example below:

[1] GP - Hyperion Planning Upgrade
[2] Application Support Renewal
[3] Oracle EBS upgrade 11.5 to 12.1
[4] Bluemix Services
[5] Cognos 11 Upgrade

I also have a list of dictionary terms called terms:

[1] "IBM"     "Oracle"     "SQL Server"     "Cognos"     "Azure"

What I need from R is to create a dataframe from the list of terms and a total count of each team from the Names factor. Example:

         term       count
1        IBM         3
2        Oracle      6
3        SQL Server  0
4        Cognos      2
5        Azure       9

Of note: the term can be in multiple times in a single name. it counts as once.

I would like to ask if anyone has any examples on this that I could derive from. Thanks.

Upvotes: 1

Views: 134

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23101

You could try this (changing the vector Names a little bit and assuming that you want case-insensitive matches):

# input
Names <- as.character(Names)
Names
#[1] IBM GP - Hyperion IBM Planning Upgrade IBM"
#[2] Application Support Renewal"               
#[3] Oracle EBS upgrade 11.5 to 12.1"           
#[4] Bluemix Services IBM"                      
#[5] Cognos 11 Upgrade"  

terms <- c("IBM",     "Oracle",     "SQL Server",     "Cognos",     "Azure")
vgrepl <- Vectorize(grepl, 'pattern', SIMPLIFY = TRUE)
df <- +(vgrepl(tolower(terms), tolower(Names))) # case insensitive

df
#     ibm oracle sql server cognos azure
#[1,]   1      0          0      0     0
#[2,]   0      0          0      0     0
#[3,]   0      1          0      0     0
#[4,]   1      0          0      0     0
#[5,]   0      0          0      1     0

colSums(df)
#    ibm     oracle sql server     cognos      azure 
#     2          1          0          1          0 

data.frame(count=colSums(df))
#           count
#ibm            2
#oracle         1
#sql server     0
#cognos         1
#azure          0

[EDIT2]

df <- data.frame(count=colSums(df))
df <- cbind.data.frame(terms=rownames(df), df)
df
#                terms count
#ibm               ibm     2
#oracle         oracle     1
#sql server sql server     0
#cognos         cognos     1
#azure           azure     0

Upvotes: 1

Zelazny7
Zelazny7

Reputation: 40618

Here's an example avoiding regex in favor of match:

names <- c(
"GP - Hyperion Planning Upgrade",
"Application Support Renewal",
"Oracle EBS upgrade 11.5 to 12.1",
"Bluemix Services",
"Cognos 11 Upgrade")

terms <- tolower(c("IBM", "Oracle", "SQL Server", "Cognos", "Azure"))

## Split your names on whitespace and match each token to the terms
counts <- lapply(strsplit(tolower(names), "\\s+"), match, terms, 0)

## index the terms using the match indices and table it
table(terms[unlist(counts)])

cognos oracle 
     1      1 

Upvotes: 1

Related Questions