I have a data.frame with a layout like this:
Data = Id somevalue
1 ab
1 cd
1 i
2 o
2 j
And I want to get index it by the Id such that i get the following:
Data = Id somevalue index
1 ab 1
1 cd 2
1 i 3
2 o 1
2 j 2
The way I do it now is with
for(ID in search_IDs)
{
Data[Data[,1]==ID,]$index<-1:length(Data[DataGuess[,1]==ID,1])
}
or more r like:
Data<-as.data.frame(sapply(Ids,FUN=(function(x,y)y[y[,1]==x,]$index<-1:length(y[y[,1]==x,1])),y=Data))
However both take a long time to finish and I was wondering if there was a faster way to make this work.