DS4PS / cpp-526-spr-2020

Course shell for CPP 526 Foundations of Data Science I for Spring 2020.
http://ds4ps.org/cpp-526-spr-2020/
3 stars 0 forks source link

Lab 05 - count() #21

Open ecking opened 4 years ago

ecking commented 4 years ago

Hello, I'm stuck on number 4s count function.

4) What is the most common type of accident (Collisionmanner) that occurs on Mondays? Use dplyr’s count() function.

I see this in help: count(x, ..., wt = NULL, sort = FALSE, name = "n", .drop = group_by_drop_default(x))

But I have no idea where to begin here. :(

Do I do something like count(dat$Collesionmanner).....

lecy commented 4 years ago

Note that dplyr functions have a syntax that is distinct from core R functions. They all have the form:

some_data_verb( df.name, arguments )

Since dplyr identifies the data frame you are working with up front, you can now call the variable names directly and do not have to use the $ operator.

count() works like the table() function in core R.

table( dat$var1, dat$var2 )   # core R
count( dat, var1, var2 )      # dplyr

One main difference is that table() returns a table object. The dplyr functions always return data frames that can then be further manipulated. For example:

count( dat, var1, var2 ) %>% arrange( - n )  # sort largest to smallest