How can I avoid clipping my xticks?

I'm plotting some data that has an associated date. However, the far most right xtick (Apr 2017) is clipped because the xtick is too long. Besides rotating or reformatting the tick, how can I avoiid this sort of clipping? ...
more »

2017-03-19 21:03 (1) Answers

mutate a column conditionally in dplyr

with a df like below, using dplyr I need to mutate the fields val1, val2, val3 conditionally. > df <- data.frame( id = c(1,2), loc=c("loc1", "loc2"), val1=c(80,64), val2=c(240,32768), val3=c(32768, 64) ) > df id loc val1 val2 v...
more »

2017-03-19 16:03 (2) Answers

Identify if in parallel call

I have a generic chunking function that breaks big calls into smaller pieces and runs them in parallel. chunk_it <- function(d, n, some_fun) { # run n chunks of d in parallel dat <- foreach(...) %doPar% { some_fun(...) } } I want t...
more »

2017-03-18 22:03 (2) Answers

Handling extra rows in colMeans calculations

I'm an occasional R-user working with several large annual datasets with one column for year and another for the location, similar to the following (which I'll call "timeseries"): Year L1 1960 1.11 1961 1.14 1962 0.75 1963 0...
more »

2017-03-18 21:03 (2) Answers

R Recover original data.frame from model.frame

In R, you can fit GAM models from the mgcv package using a formula which contains transformations such as log or sqrt and by default the model.frame is returned (only the variables specified in the formula with transformations applied). Is there any...
more »

2017-03-18 19:03 (3) Answers

R ggplot2: draw segment between points

How can I use geom_segment to draw lines on plot, after the data have been melted with reshape2? # Tiny dataset facet_group <- c("facet1", "facet1", "facet2", "facet2") time_group <- c("before", "after", "before", "after") variable1 <- c(1,...
more »

2017-03-18 17:03 (1) Answers

Limit message output when knitting R to PDF

I have a function that reports progress via message(). Under normal use, this is desirable, and the messages can be suppressed via suppressMessages() if desired. However, I'm writing a vignette (in Rmarkdown) that calls this function, and the resul...
more »

2017-03-16 16:03 (1) Answers

%in% operator vs ==, handling of NA's

I was wondering whats wrong with my code and found that the reason for my odd results was this unexpected handling of NA's by == and %in%. > NA %in% NA [1] TRUE > NA == NA [1] NA Is there a reason for this? I've been reading about the == op...
more »

2017-03-16 15:03 (0) Answers

Safely overwriting objects in R

I am working with fairly large S4 objects in R (~10GB). Often, I want to load them, do something to them, then save the results. Ideally, I would just overwrite the object using SaveRDS. For example: a = readRDS(object_file) a = do_something() saveR...
more »

2017-03-16 00:03 (0) Answers

get nearest data from dataframe in R

I have a dataframe looks like: date accumulated_visits 01-01 102 01-02 134 01-03 148 01-04 159 01-05 162 01-06 175 I want a function, which could find the nearest entry, given a specific visit number. For example, I want to fin...
more »

2017-03-15 04:03 (1) Answers

How can I optimize my random value generation?

I was given a probability distribution that I cannot find in any R packages, and I was told to generate random values from it. The probability distribution function is called the LogGamma distribution and has the following probability distribution fu...
more »

2017-03-15 00:03 (2) Answers

How to convert "A01" to "A1" using R?

How to convert "A01" to "A1" using R? As the first step I tried to get the middle character in the string but I couldn't. STR <- "A01" p <- as.character(substr(STR,2,1)) I get the following for p. I am very new to R. Please advice me. ...
more »

2017-03-14 21:03 (2) Answers

How to determine changes in a dataframe?

I would like to log changes in a data.frame: In the original dataframe I have the following structure: library(dplyr) library(compare) origianl <- data_frame( name = c('John','Tim','Allan','Mitch'), A = c(0,1,1,0), ...
more »

2017-03-14 13:03 (3) Answers

knitr: Code Externalization for Python

I use read_chunk() to read R code from external source. I wonder if there is a similar function to read Python code into main document. Any help will be highly appreciated. Thanks Edited Some extract of my .Rmd document. R works, of course. ```{r...
more »

2017-03-14 12:03 (1) Answers