For every unit increase in one column value , another column entries increase

32 views Asked by At

I have a simulation dataset with 500 replicates - each replicate contains 300 ids. When rep = 1, id ranges from 1-300; when rep = 2, id again ranges from 1-300 and so on.

I want to get the following: when rep = 1: id 1-300; when rep = 2: id 301-600 and so on. This can be easily done using an if-else statement if number of replicates is relatively small - like the following code does the job for four replicates:

d1 <- mutate(d1, ID = ifelse(rep==1, id,
ifelse(rep==2, id+300,
ifelse(rep==3, id+600, id+900))))

But how should I address this when I have 500 replicates? So essentially my question is: how should I code - for every unit increase in replicate column, the id column will increase by 300? I have attached the result for 4 replicates (the result of the above code).

Here is a snapshot of the data: replicate 1 replicate 4

1

There are 1 answers

0
Tur On

I would use rep to identify the id, like:

d1 <- mutate(d1, ID = id + 300*(rep-1))