So I have a set of data that looks like this:
group ID
aa 123
ab 123
bb 345
bb 345
bb 999
bb 999
cc 567
cd 567
When the ID is the same, the group should be equal to the second entry for that ID. So the corrected data should be this:
group ID group2
aa 123 ab
ab 123 ab
bb 345 bb
bb 345 bb
bb 999 bb
bb 999 bb
cc 567 cd
cd 567 cd
I also need to create a new variable to store the correct group. Here is what I have been trying:
n <- 1 + (1:(as.numeric(nrow(data))))
l <- 1:(as.numeric(nrow(data)))
while (data[n,1] == data[l,1]) { data$group2 <- data[n,1] }