Give a data.frame an index per value of another column in r

R.vW

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.

r2evans

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.

edited at
0

Comments

0 comments
Login to comment

Related

In R: Replacing value of a data frame column by the value of another data frame when between condition is matched

Lambda data frame reference a value in another column

Add value from one data frame into another data frame in R

Merge values of a data frame to another data frame by matching value and column name in r

Adding a column to a data frame with a distinct value per group

extract info from a column based on value from another column in data.frame r

Combining all data in a data frame per column and groups in R

Setting index using commonly occurring column value as index of the data frame

Finding value in column of data frame corresponding to two values in another column

Using a column as a column index to extract value from a data frame in R

R - find overlapping dates per group based on another data frame

Sort a data frame based on another sorted column value in R

R data frame: Change value in 1 column depending on value in another

Finding maximum value of one column (by group) and inserting value into another data frame in R

Multiply column of data frame as per value in another data frame

Aggregate a data frame per unique value of a column

How to add data from column in a data frame to a corpus based on a value from another column in R?

R: Comparing, per row in a data frame, if answers are the same as another group

R: One value per hour in data frame

Sort data frame based on the value of a column and the length of a list in another column

How to filter data in a column data frame using value in another column?

Aggregate data frame based on column value in R

Vectorized recoding of rows in R data frame based on value in another column

Filter data frame to get only rows that have a value in column and another value in any column after first value, R

Create new column based on a value of another column in a data-frame

Pandas: Calculating a value in a separate data frame column frame based on range of values in another data frame column (python)

Append column to data frame with text based on another column value

how to stack the same data frame in itself and change a value in a column per stack in r

How to multiply each column in a data frame by a different value per column