Reputation: 429
The following is an example based on a small subset of my data:
NAME <- c("SYNOP", "SYNOP", "METAR", "METAR", "SYNOP", "METAR")
AIR <- c(6.7, 8.3, 9.2, 8.9, 9.1, 8.7)
Example <- data.frame(NAME, AIR)
NAME AIR
1 SYNOP 6.7
2 SYNOP 8.3
3 METAR 9.2
4 METAR 8.9
5 SYNOP 9.1
6 METAR 8.7
I am using grep to select a subset of this data where NAME == METAR and find out the number of occurences:
ex_METAR <- Example[grep("METAR", Example$NAME), ]
nrow(ex_METAR)
I have to repeat this for a large number of instances of NAME
and wanted to speed this process up by making use of it in a function. However I must be doing something wrong as I get an error message each time:
example_Function <- function (A, B, C) {
A[grep("B", A$C), ]
}
> example_Function(Example, "METAR", Example$NAME)
[1] NAME AIR
<0 rows> (or 0-length row.names)
I have thought it was how I am describing "METAR" so I've tried the function with only A and C and get the same error.
example_Function <- function (A, C) {
A[grep("METAR", A$C), ]
}
example_Function(Example, Example$NAME)
Is there something I'm actively doing wrong or will this simply just not work? I've never tried to adapt a function in this way before. Or maybe a function is the wrong way to go?! Thanks in advance.
(Not a duplicate of Aggregate a dataframe on a given column and display another column which is looking for subsetting with maximums. I need to subset for the words in a column and know how many times that has happened.)
Upvotes: 0
Views: 103
Reputation: 547
I think this is what you are looking for :
NAME <- c("SYNOP", "SYNOP", "METAR", "METAR", "SYNOP", "METAR")
AIR <- c(6.7, 8.3, 9.2, 8.9, 9.1, 8.7)
Example <- data.frame(NAME, AIR)
library(dplyr)
Example %>% group_by(NAME) %>% summarize(Count=n())
Output :
Source: local data frame [2 x 2]
NAME Count
(fctr) (int)
1 METAR 3
2 SYNOP 3
Upvotes: 1