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.
Base R:
x1 <- do.call(
rbind.data.frame,
by(x, x$Id, function(df) { df$index <- seq_len(nrow(df)); df; })
)
x1
# Id somevalue index
# 1.1 1 ab 1
# 1.2 1 cd 2
# 1.3 1 i 3
# 2.4 2 o 1
# 2.5 2 j 2
Using dplyr
:
library(dplyr)
x2 <- x %>%
group_by(Id) %>%
mutate(index = row_number()) %>%
ungroup()
x2
# # A tibble: 5 x 3
# Id somevalue index
# <int> <chr> <int>
# 1 1 ab 1
# 2 1 cd 2
# 3 1 i 3
# 4 2 o 1
# 5 2 j 2
Your data:
x <- read.table(header=TRUE, stringsAsFactors=FALSE, text='
Id somevalue
1 ab
1 cd
1 i
2 o
2 j')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments