-2

I get a freq table, but can I save this table in a csv file or - better - sort it or extract the biggest values?

library(plyr)
count(birthdaysExample, 'month') 
  • You can use `order` or `arrange` from dplyr or use `which.max` to get the biggest values. An example data and expected result would be useful – akrun Jul 09 '15 at 07:46
  • 1
    Hello, here is some [guidance on how to make reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), which make it a lot easier to answer your questions. – andybega Jul 09 '15 at 08:20

1 Answers1

1

I'm guessing at what the relevant part of your data looks like, but in any case this should get you a frequency table sorted by values:

library(plyr)
birthdaysExample <- data.frame(month = round(runif(200, 1, 12)))
freq_df <- count(birthdaysExample, 'month')

freq_df[order(freq_df$freq, decreasing = TRUE), ]

This gives you:

   month freq
5      5   29
9      9   24
3      3   22
4      4   18
6      6   17
7      7   15
2      2   14
10    10   14
11    11   14
8      8   13
1      1   10
12    12   10

To get the highest 3 values:

library(magrittr)
freq_df[order(freq_df$freq, decreasing = TRUE), ] %>% head(., 3)
  month freq
5     5   29
9     9   24
3     3   22

Or, with just base R:

head(freq_df[order(freq_df$freq, decreasing = TRUE), ], 3)

With dplyr

dplyr is a newer approaching for many routine data manipulations in R (one of many tutorials) that is a bit more intuitive:

library(dplyr)
library(magrittr)
freq_df2 <- birthdaysExample %>% 
  group_by(month) %>% 
  summarize(freq = n()) %>% 
  arrange(desc(freq))
freq_df2

This returns:

Source: local data frame [12 x 2]

   month freq
1      5   29
2      9   24
3      3   22
4      4   18
5      6   17
6      7   15
7      2   14
8     10   14
9     11   14
10     8   13
11     1   10
12    12   10

The object it returns is not a data frame anymore, so if you want to use base R functions with it, it might be easier to convert it back, with something like:

my_df <- as.data.frame(freq_df2)

And if you really want, you can write this to a CSV file with:

write.csv(my_df, file="foo.csv")
andybega
  • 1,387
  • 12
  • 19
  • In `plyr::` What does the :: do? – Jacob Lindberg Jul 09 '15 at 11:54
  • Sorry, that's probably not necessary here. I've edited it out. Generally, `package::function` specifies that you want to use that function from that package. It's mainly, at least for me, a way to avoid conflicts when there are multiple versions of a function from different packages, e.g. look at `?filter` vs. `?dplyr::filter` – andybega Jul 09 '15 at 13:12