Unconventional data frame reshaping

I'm trying to reshape table from col1| col2|col3|id v1 | v2 | v3 |1 v2 | v1 | v3 |2 to col1|col2|id v1 | v2 | 1 v2 | v3 | 1 v2 | v1 | 2 v1 | v3 | 2 Basically create pair of following columns for each id. Sorry for not clear title, but ...
more »

2017-10-04 23:10 (5) Answers

Misunderstanding of functions in R

I have a vector p <- seq(0,1,length=11) That I want to use as the independent variable in the summation If I craft a function that codes the summation by hand f <- function(a){ a^0*(1-a)^5+a^1*(1-a)^4+... } and pass it p, then I get t...
more »

2017-10-04 22:10 (3) Answers

Convert url query key-value pairs to data frame

How can I parse url query strings, which essentially are key-value pairs into a typical data.frame? Is there a straightforward way to do this in R? Maybe with a library? Input: unique1=blub&same=aaaa-aaaa-aaaa&date=01.01.2017&time=12:3...
more »

2017-10-04 18:10 (1) Answers

Remove white spaces between letters [A-Za-z]

How to remove whitespaces between letters NOT numbers For example: Input I ES P 010 000 000 000 000 000 001 001 000 000 IESP 000 000 Output IESP 010 000 000 000 000 000 001 001 000 000 IESP 000 000 I tried something like this gsub("(?<=...
more »

2017-10-04 12:10 (2) Answers

How should I use nested ifelse statements?

I have created a data frame with the following data name <- c("A","B","C","D","E","F","G","H","I","J") age <- c(22,43,12,17,29,5,51,56,9,44) sex <- c("M","F","M","M","M","F","F","M","F","F") rock <- data.frame(name,age,sex,stringsAsFacto...
more »

2017-10-04 11:10 (5) Answers

Changing axis limits of effects plot in R

I'm using the effects package to plot interaction effects of a linear regression like this: library(effects) Model <- lm(drat~hp*cyl, data=mtcars) plot(effect(term="hp*cyl",mod=Model,default.levels=10),multiline=TRUE) How do I change the limits...
more »

2017-10-04 10:10 (3) Answers

Function operators and environments

Alright stackies, I've been reading Advanced R and have really found it helpful. I'm at a spot where I really don't understand R's behavior in the example: dot_every <- function(n, f) { i <- 1 function(...) { if (i %% n == 0) cat(".")...
more »

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

What is wrong with my function and for-loop?

I am currently trying to count the absolute number of countries in a long string. I have loaded a data frame named "countries" with column "Countries", consisting of all countries in the world. I want to make a function that searches any string, loop...
more »

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

New install doMPI throwing MPI_ERR_SPAWN error

I updated open-mpi to 3.0.0, reloaded Rmpi and doMPI, and now getting this error when executing startCluster on Ubuntu Linux, R 3.4.2. Error in mpi.comm.spawn(slave = rscript, slavearg = args, nslaves = count, : MPI_ERR_SPAWN: could not spawn pr...
more »

2017-10-03 11:10 (1) Answers

Find simple frequency pattern in dates

I have a list of customer payment dates and I'm looking to see if there is a 7/14 day or monthly pattern to the payments, often there is!. The problem is that there can also be intermediate payments of similar value, so just looking at the time betwe...
more »

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

Can't adjust cut-off levels in shading_max

Following on the question on vcd in R by rnso (How to change color palette of mosaic plot) I would like to ask what I am doing wrong when trying to adjust the cut-off levels in a shading-max mosaic? library(vcd) mat <- matrix(c(120,230,84,70,13...
more »

2017-10-03 09:10 (1) Answers

Imputing partially missing dates

I have time series data of a clinical trial, in which patients are treated within treatment sequences. Patients can have up to 10 sequences. Each sequence has a start and an end date. In addition, patients have dates for enrollment, before the first ...
more »

2017-10-02 23:10 (0) Answers

Using the nb.control argument of glmer.nb

I am working on a negative binomial model using the glmer.nb function within the lme4 package of R. The actual model itself is somewhat complicated, but should be (at least I believe) statistically sound. My question at the moment arises because the ...
more »

2017-10-02 23:10 (1) Answers

DataTables apply column formatting to filter also

When creating a datatable with filter = 'top' and also using a formatting function on a column, the formatting isn't applied to the filter control for that column. Is there a way to format the filter controls as well? For example, if I have floating...
more »

2017-10-02 21:10 (0) Answers

why is "<integer>" == <integer> true in R

I just started learning R and in my first assignment, I face a problem where I need to compare a bunch of variables and while doing that I am supposed to get false when comparing two variables not only when they are not equal but also when their type...
more »

2017-10-02 17:10 (1) Answers

R vectorization implementation in a function

I am having trouble to understand how to take advantages of R vectorization to do what I wanted in a more efficient way. Briefly, for each line, I want to compare the 2nd column ($start) of current line with $start of the previous and next line (al...
more »

2017-10-02 14:10 (1) Answers

How to compare lists in a dataframe

I have a dataframe as follows: Input one<-c("Rainy and sunny;thundering;lightning","dismal and dreary;thundering") two<-c("Overcast;lightning","Overcast;dismal and dreary") df2<-data.frame(one,two) I want to compare the strings in the li...
more »

2017-10-02 13:10 (2) Answers

count the levels by groups

I have a question about check which level is the most. Here is my data: Year Area V1 V2 V3 2014 USA 100 25 50 2014 USA 200 50 60 2014 USA 200 50 50 2014 USA 200 50 50 2014 USA 300 75 40 2...
more »

2017-10-02 09:10 (2) Answers