I have a column in my dataframe as follows
Col1
----------------------------------------------------------------------------
Center for Animal Control, Division of Hypertension, Department of Medicine
Department of Surgery, Division of Primary Care, Center for Animal Control
Department of Internal Medicine, Division of Hypertension, Center for Animal Control
How do I count the number of strings that occur that is separated by a comma, in other words what I am trying to accomplish is something like this below
Affiliation Freq
------------------------------------------
Center for Animal Control 3
Division of Hypertension 2
Department of Medicine 1
Department of Surgery 1
Division of Primary Care 1
Department of Internal Medicine 1
Could someone help me to figure this out?
Assumption:
Center for Animal Control, Division of Hypertension, Department of Medicine
is value for row 1,Department of Surgery, Division of Primary Care, Center for Animal Control
for row 2 and so on.df
is the data frame.The answer is
I use
scan
andtrimws
for these text processing tasks.Can also wrap as.data.frame around that result:
Here is one approach. Also substitute
'\n'
with a comma since you have some new lines in your text.Output as follows (on original data):