Error: x must be atomic for ‘sort.list’

From the output of str(cc2), the variable inside of the data.table, V1, is itself a list. This means that cc2 is a nested list of length 1. The error is occurring because table calls sort.list, which requires an atomic vector as input. Try using unlist: unlist will (recursively) extract elements from their list containers. So unlist(cc2) will return a vector, which works directly … Read more

Code to clear all plots in RStudio

dev.off() closes the current graphical device. This clears all of the plots for me in RStudio as long as I don’t have a different graphical device open at the moment. If you do have other graphical devices open then you can use dev.list() to figure out which graphical device is RStudio’s. The following should do that but I haven’t tested … Read more

Construct a manual legend for a complicated plot

You need to map attributes to aesthetics (colours within the aes statement) to produce a legend. I understand where Roland is coming from, but since this is only 3 attributes, and complications arise from superimposing bars and error bars this may be reasonable to leave the data in wide format like it is. It could … Read more

Use of ~ (tilde) in R programming Language

The thing on the right of <- is a formula object. It is often used to denote a statistical model, where the thing on the left of the ~ is the response and the things on the right of the ~ are the explanatory variables. So in English you’d say something like “Species depends on Sepal Length, Sepal Width, Petal Length and Petal Width”. … Read more

How To Create Vector of Vector In R

Use a list: Think of it as a map/dictionary/associative array that is being indexed by an integer. And if you want to take a string like the one above and turn it into a list of vectors: I’m using strsplit to split by newlines, then applying strsplit again to each line. The as.numeric is there … Read more

How to access the last value in a vector?

Suppose I have a vector that is nested in a dataframe one or two levels. Is there a quick and dirty way to access the last value, without using the length() function? Something ala PERL’s $# special var? So I would like something like: instead of