Rotating and spacing axis labels in ggplot2

Change the last line to q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead: The image above is from this blog … Read more

data.table vs dplyr: can one do something well the other can’t or does poorly?

We need to cover at least these aspects to provide a comprehensive answer/comparison (in no particular order of importance): Speed, Memory usage, Syntax and Features. My intent is to cover each one of these as clearly as possible from data.table perspective. Note: unless explicitly mentioned otherwise, by referring to dplyr, we refer to dplyr’s data.frame … Read more

Drop data frame columns by name

You can use a simple list of names : DF <- data.frame( x=1:10, y=10:1, z=rep(5,10), a=11:20 ) drops <- c(“x”,”z”) DF[ , !(names(DF) %in% drops)] Or, alternatively, you can make a list of those to keep and refer to them by name : keeps <- c(“y”, “a”) DF[keeps] EDIT : For those still not acquainted … Read more

Remove rows with all or some NAs (missing values) in data.frame

Also check complete.cases : > final[complete.cases(final), ] gene hsap mmul mmus rnor cfam 2 ENSG00000199674 0 2 2 2 2 6 ENSG00000221312 0 1 2 3 2 na.omit is nicer for just removing all NA‘s. complete.cases allows partial selection by including only certain columns of the dataframe: > final[complete.cases(final[ , 5:6]),] gene hsap mmul mmus … Read more

Grouping functions (tapply, by, aggregate) and the *apply family

R has many *apply functions which are ably described in the help files (e.g. ?apply). There are enough of them, though, that beginning useRs may have difficulty deciding which one is appropriate for their situation or even remembering them all. They may have a general sense that “I should be using an *apply function here”, … Read more

How to join (merge) data frames (inner, outer, left, right)

By using the merge function and its optional parameters: Inner join: merge(df1, df2) will work for these examples because R automatically joins the frames by common variable names, but you would most likely want to specify merge(df1, df2, by = “CustomerId”) to make sure that you were matching on only the fields you desired. You … Read more

How to make a great R reproducible example

Basically, a minimal reproducible example (MRE) should enable others to exactly reproduce your issue on their machines. Please do not post images of your data, code, or console output! tl;dr A MRE consists of the following items: a minimal dataset, necessary to demonstrate the problem the minimal runnable code necessary to reproduce the issue, which … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)