Esther
Esther

Reputation: 300

Equivalent of Stata's expand in R

Whilst reviewing a colleague's Stata code I came across the command expand.

I would really love to be able to do the same thing simply in my own R code.

Essentially expand duplicates a dataset n times but has the option to create a new variable which is 0 if the observation originally appeared in the dataset and 1 if the observation is a duplicate.

Does anyone know of a quick way of implementing this in R? Or is it a case of writing my own function?

Upvotes: 2

Views: 1774

Answers (2)

user2838808
user2838808

Reputation: 55

This function expands the rows of a data.frame like the Stata expand command does. I got the idea from the R mefa package.

expand_r <- function(df, ...) {
  as.data.frame(lapply(df, rep, ...))
}

df <- data.frame(x = 1:2, y = c("a", "b"))

expand_r(df, times = 3)

Upvotes: 1

tushaR
tushaR

Reputation: 3116

rep_r<-function(x,n){if(n<=1){rep(x,times=1)}else{rep(x,times=n)}}

expand_r<-function(x,n){
    Reduce(function(x,y)
        {c(x,y)},mapply(rep_r,x,n))
}

expand_r(c(2,3,4,1,5),c(-1,0,1,2,3))
#[1] 2 3 4 1 1 5 5 5

EDIT: Thanks to the suggestion from @nicola the above functionality can be simply achieved by the following one-liner.

expand_r<-function(x,n) rep(x,replace(n,n<1,1))
#>expand_r(c(2,3,4,1,5),c(-1,0,1,2,3))
#[1] 2 3 4 1 1 5 5 5

Upvotes: 2

Related Questions