Blog Archives

%$% : upping your pipe game

February 10, 2020
By
%$% : upping your pipe game

Problem What do I do when %__% doesn’t work? Context I love the %__%  pipe. Originally from magrittr, it’s now characteristic of tidy code. Using %__% has revolutionized how I write code in R (pssst! coming soon: an interactive pipe tutorial!). But sometimes the basic pipe falls short. table() is one of my favorite functions … Continue reading "%$%...

Read more »

Some lessons from rstudio::conf

February 3, 2020
By
Some lessons from rstudio::conf

Today I’m departing a little from the problem/context/solution format of these posts to share some things I learned from last week’s rstudio::conf. When I started in R a few years ago, I never thought I would have any place at a coding conference for computer people. But thanks to some help from my lab and my … Continue reading "Some...

Read more »

if ifelse() had more if’s, AND an else

November 22, 2019
By
if ifelse() had more if’s, AND an else

Problem The case_when() function in dplyr is great for dealing with multiple complex conditions (if’s). But how do you specify an “else” condition in case_when()? Context Last month, I was super excited to discover the case_when() function in dplyr. But when I showed my blog post to a friend, he pointed out a problem: there seemed to … Continue reading "if...

Read more »

Loading packages efficiently

October 11, 2019
By
Loading packages efficiently

Problem Especially in a project with many different scripts, it can be challenging to keep track of all the packages you need to load. It’s also easy to lose track of whether or not you’ve incorporated package loading into the script itself until you switch to a new computer or restart R and all of … Continue reading "Loading...

Read more »

if ifelse() had more if’s

October 11, 2019
By
if ifelse() had more if’s

Problem The ifelse() function only allows for one “if” statement, two cases. You could add nested “if” statements, but that’s just a pain, especially if the 3+ conditions you want to use are all on the same level, conceptually. Is there a way to specify multiple conditions at the same time? Context I was recently … Continue reading "if...

Read more »

Initializing an empty list

September 13, 2019
By

Problem How do I initialize an empty list for use in a for-loop or function? Context Sometimes I’m writing a for-loop (I know, I know, don’t use for-loops, but sometimes it’s just easier. I’m a little less good at apply functions than I’d like to be) and I know I’ll need to store the output … Continue reading "Initializing...

Read more »

(Automatically Show Output)

July 22, 2018
By
(Automatically Show Output)

Problem It’s annoying to have to type the name of an object I just created in order to print its output in a script. Context A certain lightsaber-wielding stats professor of mine liked to point out that R doesn’t go out of its way to be helpful. If you write a line of code that … Continue reading "(Automatically...

Read more »

prop.table()

July 22, 2018
By
prop.table()

Problem How can I convert a frequency table into proportions? Context This is a continuation of the data manipulation discussed in the ​`​with()` post. I had just finished making a table # Load data from GitHub polygon

Read more »

with( )

July 20, 2018
By
with( )

Problem Making graphics with base R is annoying for many reasons, but a big one is having to type the name of the data frame over and over again to reference different columns. Context Back to our Mississippi River fish data. I’ve aggregated my sampling points into polygons, and now I want to explore some … Continue reading "with(...

Read more »

Changing individual column names

July 11, 2018
By

Problem How do I change the name of just one column in a data frame? Context This is a simple one that keeps coming up. Sometimes, whoever put together my data decided to capitalize the first letter of some column names and not others. Sometimes I’ve merged several data frames together and I need to … Continue reading "Changing...

Read more »

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)