Understanding the result of modulo operator: %%

Nothing wrong: The modulo is the number after +. In general, for two numbers a and b, there is Let’s write a toy function: Update: Instead of using floor(a / b) to get quotient, we can also use a %/% b.

Is it possible to have a multi-line comments in R?

You can, if you want, use standalone strings for multi-line comments — I’ve always thought that prettier than if (FALSE) { } blocks. The string will get evaluated and then discarded, so as long as it’s not the last line in a function nothing will happen. The main limitation is that when you’re commenting stuff out, you’ve … Read more

Convert data.frame columns from factors to characters

Just following on Matt and Dirk. If you want to recreate your existing data frame without changing the global option, you can recreate it with an apply statement: This will convert all variables to class “character”, if you want to only convert factors, see Marek’s solution below. As @hadley points out, the following is more concise. … Read more

How do you use “<<-" (scoping assignment) in R?

<<- is most useful in conjunction with closures to maintain state. Here’s a section from a recent paper of mine: A closure is a function written by another function. Closures are so-called because they enclose the environment of the parent function, and can access all variables and parameters in that function. This is useful because it allows us … Read more

Categories r

R multiple conditions in if statement

Read this thread R – boolean operators && and ||. Basically, the & is vectorized, i.e. it acts on each element of the comparison returning a logical array with the same dimension as the input. && is not, returning a single logical.

ggplot geom_text font size control

Here are a few options for changing text / label sizes The size in the geom_text changes the size of the geom_text labels. For this And why size of 10 in geom_text() is different from that in theme(text=element_text()) ? Yes, they are different. I did a quick manual check and they appear to be in the ratio of ~ (14/5) for geom_text sizes to theme sizes. … Read more

Producing subscripts in R markdown

Since you mention Pandoc in your comments, maybe it’s not cheating to depend on Pandoc’s extensions for subscript and superscript. From here, we can create a minimal example Rmd file: Using Knitr results in an HTML file that renders like this: That clearly doesn’t work. But you can run pandoc on the resulting markdown file (which I’ve named “Subscripts.md”): … Read more

Categories r