I have a database where the first row of each of the unique identifiers is missing. Basically, I need to add a new line consisting of 0 for each unique identifier.
My database looks like this (I have over a million rows, so loops are basically impossible).
dt = as.data.frame( rbind(c('A1', '15', '1'),
c('A1', '17', '2'),
c('A1', '12', '3'),
c('B1', '3', '1'),
c('B1', '4', '2'),
c('B1', '15', '3')))
colnames(dt) = c('id', 'activity', 'time')
For each id, I need to add line 0 at time 0.
The following line of codes works, but it takes too much time for my database.
IdUnique = length(unique(dt$id))
VeK = vector('list', IdUnique)
for(i in 1:IdUnique){
row0 = matrix(0, nrow = 1, ncol = ncol(dt), dimnames = list(unique(dt$id)[i], colnames(dt)))
VeK[[i]] = rbind(row0, subset(dt, id == unique(dt$id)[i]) )
VeK[[i]][,'id'] <- unique(dt$id)[i]
}
dt2 <- do.call("rbind", VeK)
I was wondering if there was a more economical solution, for example, merging strings and by id. But I can’t figure out how to do this.
mat = matrix(0, nrow = length(unique(dt$id)), ncol = ncol (dt) )
colnames(mat) <- colnames(dt)
mat[, 'id'] <- as.character(unique(dt$id))
mat <- as.data.frame(mat)
merge(mat, dt, by = 'id' )
Any solutions for row combining and id management?