Reputation: 149
I have a column in a data frame which has salutation ,i.e. Title .
When i take summary of the column I get the summary like below:
DR. DR MS. Ms. MS
--------------------
6 20 31 12 21
I planned to merge DR. as DR and MS. Ms. a MS.
So the code I wrote was
gsub("DR\\.", "DR",df$TITLE)
gsub("M[s/S]\\.","MS",df$TITLE)
but by this am not able to get the summary.... result of summary turns to be :
Length Class Mode
----------------------------
6189 character character
wherein I wanted the result of the summary to be
DR MS
--------
26 64
How do I achieve this. Whats the error I am doing here ?
Upvotes: 0
Views: 525
Reputation: 887183
We could use fixed = TRUE
and change the 'TITLE' to upper case so that we have only two categories
df$TITLE <- factor(toupper(sub(".", "", df$TITLE, fixed = TRUE)))
When we apply sub/gsub
, the factor
class changes to character
and summary
gives frequency/count for factor
class and Length
for character
class columns. So, we need to wrap it with factor
or use table
table(df$TITLE)
summary(df$TITLE)
Upvotes: 1