Brief: how can I add m
rows to my m X n
data frame, where each new row is inserted after each existing row? I will essentially copy the existing ro
Here's one possible approach if I understand what you're doing:
dat <- head(CO2, 10) # fake data set
L1 <- lapply(1:nrow(dat), function(i) {
dat2x <- dat[i, ]
dat2x[4] <- dat[i, 4] - dat[i, 5]
rbind(dat[i, ], dat2x)
})
do.call(rbind, L1)
EDIT: totally working off e4e5f4's excellent response:
new <- dat[rep(1:nrow(dat),1,each=2),]
new[c(F, T),4] <- dat[4] - dat[5]
Both are equivalent but I assume the alter is way faster.