I would like to add an empty row after each 8 rows in my whole dataset. The actual one is like :
original dataset
and the final result I would like to get is :
expected dataset
Thanks in advance.
I would like to add an empty row after each 8 rows in my whole dataset. The actual one is like :
original dataset
and the final result I would like to get is :
expected dataset
Thanks in advance.
cur <- rbind(df[1:8,], NA)
for(i in seq(from = 9, to = length(df), by = 8) {
cur <- rbind(df[i:(i+7),], NA)
}
This answer is for numeric columns.
Initially, for the first eight observations, you can generate a matrix that is a vector of ones with length 8 and an additional NA column: v<- c(rep(1, 8),NA)
and then you can kronecker product this matrix with the original: kron(a,v)
(after applying as.matrix
on both a
and v
).
this should be extendable for each 8th row using a loop.
For the character column, you can first separate it, rbind
a NA
row to each 8th element using a loop defined by seq
and then cbind
it back to the numeric matrix.