Dplyr

dplyr and the design effect in survey samples

For those guys like me who are not such R geeks, this trick could be of interest. The package dplyr can be very useful when it comes to data manipulation and you can extract valuable information from a data frame. For example, when using if you want to count how many humans have a particular hair color, you can run the following piece of code: library(dplyr) starwars %>% filter(species == "Human") %>% group_by(hair_color) %>% summarise(n = n()) hair_color n auburn 1 auburn, grey 1 auburn, white 1 black 8 blond 3 brown 14 brown, grey 1 grey 1 none 3 white 2 As a result the former query gives you a data frame and you can use it to make another query.

Automatic output format in Rmarkdown

I am writing a Rmarkdown document with plenty of tables, and I want them in a decent format, e.g. kable. However I don’t want to format them one by one. For example, I have created the following data frame in dplyr. library(dplyr) data("iris") iris %>% group_by(Species) %>% summarise(n = n()) %>% arrange(desc(n)) ## # A tibble: 3 x 2 ## Species n ## <fctr> <int> ## 1 setosa 50 ## 2 versicolor 50 ## 3 virginica 50 One solution to the output format of this data frame would be to name it as an object in R, and then give it a format by using the kable function.
UpDog logo Host your own website for free with UpDog.