Create a variable that identifies the original data.frame after rbind command in R

后端 未结 6 577
后悔当初
后悔当初 2020-12-11 06:07

I am relatively new to R and I would like to know how can I create a variable (number sequence) that identifies the each of the original data.frames before being joined with

6条回答
  •  北海茫月
    2020-12-11 06:45

    Thanks all! I ended up with a simple solution working with a friend of mine by creating an index, like this:

    index<-rep(1,times=nrow(data.frame))
    
    for (i in 1:(nrow(data.frame)-1)){
    
    if (data_frame$ID [i+1]<= data.frame$ID[i]) {
    index[i+1]<-index[i]+1
    }
    else {index[i+1]<-index[i]}}
    
    new.data.frame <- cbind(index, data.frame)
    

提交回复
热议问题