Articles by kaijagahm

%$% : upping your pipe game

February 10, 2020 | kaijagahm

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 ...
[Read more...]

Some lessons from rstudio::conf

February 3, 2020 | kaijagahm

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 ...
[Read more...]

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

November 22, 2019 | kaijagahm

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 ...
[Read more...]

Loading packages efficiently

October 11, 2019 | kaijagahm

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 ...
[Read more...]

if ifelse() had more if’s

October 11, 2019 | kaijagahm

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 ...
[Read more...]

Initializing an empty list

September 13, 2019 | kaijagahm

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 ... [Read more...]

(Automatically Show Output)

July 22, 2018 | kaijagahm

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 ...
[Read more...]


July 22, 2018 | kaijagahm

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 | kaijagahm

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 ...
[Read more...]

Changing individual column names

July 11, 2018 | kaijagahm

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 ... [Read more...]

The %notin% operator

July 8, 2018 | kaijagahm

Problem I keep forgetting how to select all elements of an object except a few, by name. I get the ! operator confused with the - operator and I find both of them less than intuitive to use. How can I negate the %in% operator? Context I have a data frame ... [Read more...]

Where are my NA’s?

July 5, 2018 | kaijagahm

Problem How can I (quickly and intuitively) figure out how many NA’s are in my dataset and which columns they’re in? Context When I tried to run PCA (Principal Components Analysis) on some USGS fish sampling data, I noticed that I had a bunch of missing values. PCA ... [Read more...]

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)