Blog Archives

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 »

The %notin% operator

July 8, 2018
By

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 called electrofishing that contains observations … Continue reading "The...

Read more »

Where are my NA’s?

July 5, 2018
By

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 needs complete observations, so this was a … Continue reading "Where...

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)