0

I have a row of strings like this :

[1] X                                             Royal.Perth.Hospital                         
[3] X.1                                           X.2                                          
[5] X.3                                           X.4                                          
[7] X.5                                           X.6                                          
[9] Fremantle.Hospital                            X.7                                          
[11] X.8                                          X.9                                          
[13] X.10                                         X.11                                         
[15] X.12                                          
Princess.Margaret.Hospital.For.Children      
[17] X.13                                         X.14                                         
[19] X.15                                          X.16                                         
[21] X.17                                          X.18                                         
[23] King.Edward.Memorial.Hospital.For.Women       X.19                                         
[25] X.20                                          X.21                                         
[27] X.22                                          X.23                                         
[29] X.24                                          Sir.Charles.Gairdner.Hospital                
[31] X.25                                          X.26                                         
[33] X.27                                          X.28                                         
[35] X.29                                          X.30                                         
[37] Armadale.Kelmscott.District.Memorial.Hospital X.31                                         
[39] X.32                                          X.33                                         
[41] X.34                                          X.35                                         
[43] X.36                                          Swan.District.Hospital                       
[45] X.37                                          X.38                                         
[47] X.39                                          X.40                                         
[49] X.41                                          X.42                                         
[51] Rockingham.General.Hospital                   X.43                                         
[53] X.44                                          X.45                                         
[55] X.46                                          X.47                                         
[57] X.48                                          Joondalup.Health.Campus                      
[59] X.49                                          X.50                                         
[61] X.51                                          X.52                                         
[63] X.53                                          X.54                                         

I want to count the number of times the word Hospital occured in the row. Note: the last hospital does not have the word "hospital" in it's name instead it has a 'health campus' in it's name.

I tried using the function

occurences<-table(unlist(myrow))
occurences["Hospitals"]

but couldn't count the number of hospitals in the row.

The output should look like :

Hospitals : 8
Health campus: 1

Total Hospitals = 9
Ahsan Asif
  • 31
  • 8

2 Answers2

1

One solution is to count the number of occurences of "Hospital" in each string and then take the sum.

Try this:

library(tidyverse)

strings <- c("X", "Royal.Perth.Hospital","X.1","X.2","Rockingham.General.Hospital")

strings %>% str_count("Hospital") %>% sum()
0

We could use grepl to find occurrence of "Hospital" and "Health.Campus" and then combine them to find occurrence of both.

Hospitals <- sum(grepl("Hospital", occurences))
Health.Campus <- sum(grepl("Health.Campus", occurences))
Total <- Hospitals + Health.Campus

You might want to include ignore.case = TRUE in grepl if the words occur in both lower and upper case.


Instead of grepl you could also use stringr::str_detect in similar fashion.

Hospitals <- sum(stringr::str_detect(occurences, "Hospital"))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213