Reputation: 95
I have two files of data and I am going to do the group numbering of the first file based on the numbers in the second file.(like the image)
graphics.off()
rm(list=ls())
df110<-read.csv('C:/Users/Shel3/Desktop/tmr/D1.csv')
df210<-read.csv("C:/Users/Shel3/Desktop/tmr/S1.csv")
mat1<-array(, dim=c(nrow(df110), 1))
b<-cbind(mat1, df110)
i<-1
for(i in 1:nrow(b)){
for(j in 1:nrow(df210)){
df210[j,2]
b[i:df210[j,2],2] = j
i=df210[j,2]+1
}
}
the problem is that the program starts to run but it seems that it never ends the processing which for this simple process is very strange!! Does anyone can please help me how to solve it.
Thanks a lot in advance :)
Upvotes: 0
Views: 44
Reputation: 24079
You are changing the value of the "i" index variable inside your loop this is bad programming practice.
It looks like you are trying create a sequence of numbers based on file2. The rep()
can preform this in one line.
index<-c(1:5)
repeats <-c(2, 1, 3, 2, 4)
rep(index, times=repeats)
#[1] 1 1 2 3 3 3 4 4 5 5 5 5
Upvotes: 1