0

I have a dataframe that looks like:

 example<- data.frame(interact= c("A_X", "", "", "D_W", "E_Q", "A_X"), 
                      region= seq(1,6, 1), loc= seq(20, 25, 1))

I would like to summarize the freqency of the interact column but only for those with entries. My current method is returning counts of blank rows.

Here is the output for the example I would like:

output<- data.frame(interact= c("A_X","D_W", "E_Q"), 
                      freq= c(2, 1,1))

Here is the code I have tried, but it returns the sum of the blank rows as well:

output<-example %>%
   group_by(interact) %>%
   summarise(freq = n())
Danielle
  • 785
  • 7
  • 15
  • 3
    Can't you simply filter out the the blank rows ? `example %>% filter(interact != "") %>% count(interact)` – Steven Beaupré Jul 18 '17 at 22:27
  • That works! How simple...Do you have a suggestion regarding how to incorporate arguments that name the count column `freq` rather than `n`? – Danielle Jul 18 '17 at 23:14
  • `... %>% rename(freq = n)` – Gregor Thomas Jul 18 '17 at 23:25
  • 1
    Tempted to close as duplicate of [how to subset data based on field](https://stackoverflow.com/a/3445619/903061) – Gregor Thomas Jul 18 '17 at 23:26
  • Thank you, however I get the following error: `Error in rename(., freq = n) : unused argument (freq = n)` – Danielle Jul 18 '17 at 23:32
  • Really? Works fine for me on your sample data. Are you sure you didn't load `plyr` after `dplyr` and ignore the warning about function masking? Try `... %>% dplyr::rename(freq = n)` to make sure you are using the `dplyr` version. – Gregor Thomas Jul 18 '17 at 23:36
  • I didn't have plyr loaded, however your solution still work. Maybe tidyverse loads plyr ? I had that one opened. Thank you – Danielle Jul 18 '17 at 23:40
  • Other packages probably also have `rename` functions. `tidyrverse` doesn't load `plyr`, but some other packages might. You can use `sessionInfo` to see what packages are attached, and `?rename` may give you options of which `rename` you want to see the help for. – Gregor Thomas Jul 19 '17 at 00:00
  • 1
    Thank you for explaining that to me...I am learning more and more about R, stackoverflow has been a great resource because people like you explain how the R language works, without it is like getting a poor exam grade and not understanding why or how you can improve. I guess for many of us, this equates stackoverflow to office hours. – Danielle Jul 19 '17 at 00:23

0 Answers0