I'm trying to only keep the first 4 words of a column in my data and still want to keep the other observations that have less than 4 words.
This is a sample of what some of the data looks like.
State | Company | Number of workers |
---|---|---|
X | FAIRFIELD NURSING AND REHABILITATION CENTER, | 99 |
Y | ATHENAHEALTH | 24 |
Z | DRS TEST & ENERGY MANAGEMENT, | 1009 |
W | AMERICAN APPAREL | 376 |
C | BERRY PLASTICSPANY -ALENCE SPECIALTY ADHES | 67 |
A | TUSCALOOSA RESOURCES , SWANN'S CROSSING MINE | 456 |
I've used the following code
library(stringr)
df$Company1 <- word(df$Company, 1, 4)
While this is providing column of 4 word company names, this is not working for me because it is getting rid of the companies that have less than 4 words returning NA for those instead.
So I'm hoping to find a solution to keep every observations that has 1 to 4 words.