Create an empty data.frame

Just initialize it with empty vectors: Here’s an other example with different column types : N.B. : Initializing a data.frame with an empty column of the wrong type does not prevent further additions of rows having columns of different types.This method is just a bit safer in the sense that you’ll have the correct column types from the beginning, … Read more

Categories R

not a Stata version 5-12 .dta file

Had the same problem and Roland’s suggestion of using read_dta from the haven package worked! To reiterate Roland’s comment that helped form this solution. read.dta from the foreign package can only read files from Stata version 5 to 12 and your file seems to be created by a version outside that range. haven’s read_dta can read dta files outside … Read more

Categories R

invalid type (list) for variable

tl;dr rows of data frames are lists, not numeric vectors. When you read.table() you get a data frame (so constructing a matrix, as I did before, doesn’t replicate the problem). The fact that this is a list, not a numeric vector, is a problem. There are a variety of ways of handling this. The easiest is unlist():

Categories R

What does the double percentage sign (%%) mean?

The “Arithmetic operators” help page (which you can get to via ?”%%”) says ‘%%’ indicates ‘x mod y’ which is only helpful if you’ve done enough programming to know that this is referring to modular division, i.e. integer-divide x by y and return the remainder. This is useful in many, many, many applications. For example (from @GavinSimpson in comments), %% is useful if … Read more

Categories R

R: “Unary operator error” from multiline ggplot2 command

It looks like you might have inserted an extra + at the beginning of each line, which R is interpreting as a unary operator (like – interpreted as negation, rather than subtraction). I think what will work is Perhaps you copy and pasted from the output of an R console? The console uses + at the start of the line when … Read more

Categories R

Remove grid, background color, and top and right borders from ggplot2

EDIT Ignore this answer. There are now better answers. See the comments. Use + theme_classic() EDIT This is a better version. The bug mentioned below in the original post remains (I think). But the axis line is drawn under the panel. Therefore, remove both the panel.border and panel.background to see the axis lines. Original post This gets close. There was a bug … Read more

Categories R

Sum rows in data.frame or matrix

I have a very large dataframe with rows as observations and columns as genetic markers. I would like to create a new column that contains the sum of a select number of columns for each observation using R. If I have 200 columns and 100 rows, I would like a to create a new column … Read more

Categories R