Riccardo
Riccardo

Reputation: 97

Search for particular words in frequency table

I've been struggling to search for specific words in my frequency table, containing a bunch of University and Company names. I'd like to split this table into 2 table (or list) of 1-"Universities" and 2-"Companies". To do this, I'm thinking of searching for specific words of "University" "School" "College", and writing them all into table (or list) called "Universities". The remainder of entries of the table (presumably "Company" names) will form the 2nd table (or list) named "Companies"

I looked into this and found grep("University", OriginalFrequencyTable) isn;t working. I suppose the reason is because grep works with a character vector and not a table(?!)

OriginalFrequencyTable: (number in front of names is frequency of occurrence)

Loyalist College 2 
Globe Inc 4
University Of Central Arkansas 3
Anderson University 2
Bridgewater State College 1
Allegheny College 1
Cs Technologies 3
Healthpartners 1

Expected result (are 2 tables named, "Universities" and "Companies"):

--------------1st table "Universities":--------------
Loyalist College 2 
University Of Central Arkansas 3
Anderson University 2
Bridgewater State College 1
Allegheny College 1

--------------2nd table "Companies":--------------
Globe Inc 4
Cs Technologies 3
Healthpartners 1

Any help is highly appreciated, Many thanks in advance,

Upvotes: 2

Views: 113

Answers (2)

akrun
akrun

Reputation: 887213

We can use split to split the data.frame into a list of data.frame

library(stringr)
lst1 <- split(df, str_detect(df$Name, "\\b(College|University)\\b"))

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521437

One base R option would be to use grepl and search for the keywords College or University as markers of a school:

Universities <- df[grepl("\\b(College|University)\\b", df$Name), ]
Companies <- df[!grepl("\\b(College|University)\\b", df$Name), ]

I am not sure what your column(s) is called, by I assumed there is a Name column containing the company and university names. If there be an actual separate column for the frequency, then the above solution should still work. It should also work if you have the name and frequency together in a single column.

Upvotes: 0

Related Questions