Subject ▸ rstats

Piping ggplot2 objects into plotly

The pipe operator (|>) offers a way to write cleaner and more readable code in R, and many are familiar with piping objects into functions. However, creating interactive graphs with plotly package is less straightforward than most cases. When you try to pipe a ggplot2 object directly into ggplotly() from the plotly packge without storing it in a variable or using curly braces, you will encounter an error. This happens because the pipe operator passes the output of the left-hand side expression as the first argument to the function on the right-hand side.

Read More…

Twitter data in R

Update (November 2024): Since the publication of this blogpost, Twitter has revised its API access policies, and it is no longer freely accessible in the same way it once was. As a result, some of the workshop slides are now out of date. Slides | All course materials Earlier this month, I taught my two-day course on working with Twitter data in R, at the University of Lucerne. This was part of a Master’s Programme in Computational Social Science, LUMACSS.

Read More…

Web scraping in R

Click here for the slides, and here for all the course materials. I recently organised a short course on web scraping in R, as part of a Master’s Programme in Computational Social Science, at the University of Lucerne. I have built a website and a Shiny app just for this course, to facilitate learning. These are tailored for the exercises in the course. You can find other course material at GitHub.

Read More…

R Markdown workshop

Click here for the slides, and here for all the workshop materials. R Markdown has been at the centre of my research workflow for some time. It allows me to tidy and analyse data, create tables and figures, manage citations and references, and write up the results in one screen. And if, say, a regression table needs a new model, it often takes only a few lines of code and a click to reproduce the output — be it a PDF, HTML, and/or a Word document.

Read More…

Calculating standard deviations for survey subgroups in R

The survey package is one of my favourites in R. Among its many other uses, it can compute summary statistics by subgroups. For example, if you have a survey of individuals from several countries with an item on the respondents’ income, you can calculate the average income in each subgroup with the svyby() function. However, like many other functions in the package, svyby() returns standard errors—but not standard deviations—of the mean values.

Read More…