2
votes

I'd appreciate help on the following data-wrangling task. I have a data frame that looks like this:

df <- data.frame(var1=c('a', 'b', 'c'), var2=c('d', 'e', 'f'), var3=c('h', 'j', 'k'))

I want to replicate each row of data with var1 and var2 fixed number of times, let's say 3 times, but not var3. In place of missing values in var3 I want to have 0s.

In the end, I would like to have this:

    df.expanded
  var1 var2 var3
1    a    d   h
2    a    d   0
3    a    d   0
4    b    e   j
5    b    e   0
6    b    e   0
7    c    f   k
8    c    f   0
9    c    f   0
2

2 Answers

4
votes

This is indeed a close duplicate to the linked question, with the main difference being the expected values in "var3". Keeping that in mind, here's how I would approach this:

## Expand
df.expanded <- df[rep(rownames(df), each = 3), ]

## Replace
df.expanded[["var3"]] <- as.character(df.expanded[["var3"]])
df.expanded[["var3"]][c(FALSE, TRUE, TRUE)] <- 0

## View
df.expanded
#     var1 var2 var3
# 1      a    d    h
# 1.1    a    d    0
# 1.2    a    d    0
# 2      b    e    j
# 2.1    b    e    0
# 2.2    b    e    0
# 3      c    f    k
# 3.1    c    f    0
# 3.2    c    f    0

Here's a possible approach using "data.table":

library(data.table)
df.expanded <- as.data.table(df)[rep(sequence(nrow(df)), each = 3)][
  , var3 := replace(as.character(var3), .I %% 3 != 1, 0)][]

df.expanded
#    var1 var2 var3
# 1:    a    d    h
# 2:    a    d    0
# 3:    a    d    0
# 4:    b    e    j
# 5:    b    e    0
# 6:    b    e    0
# 7:    c    f    k
# 8:    c    f    0
# 9:    c    f    0
2
votes

You can try the following code (work for me) :

df <- data.frame(var1=c('a', 'b', 'c'), var2=c('d', 'e', 'f'), var3=c('h', 'j', 'k'))

for(c in names(df)) df[,c] <- as.character(df[,c])

rep_time <- 3
res <- NULL

for(i in 1:length(df$var1)) {
  temp <- cbind(rep(df$var1[i],rep_time),rep(df$var2[i],rep_time),c(df$var3[i],rep(0,rep_time-1)))
  res <- rbind(res,temp)
}

res <- data.frame(res)

Hope that will help

Gottavianoni