Visser Visser - 1 year ago 99
R Question

Incorrect function output

The following is an example based on a small subset of my data:

AIR <- c(6.7, 8.3, 9.2, 8.9, 9.1, 8.7)
Example <- data.frame(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), ]

I have to repeat this for a large number of instances of
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)
<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.)

Answer Source

I think this is what you are looking for :

AIR <-  c(6.7, 8.3, 9.2, 8.9, 9.1, 8.7)
Example <- data.frame(NAME, AIR)


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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download