3

I have this data

cluster<-c(1,1,1,1,2,2,2,2)
structure<-c(1,1,2,3,1,1,1,2)
str<-data.frame(clusters,structures)

i want to create a third column called serial numbers based on the cluster and structure. this is in such a way that i get the following output

serial.number<-c(1,2,1,1,1,2,3,1)
str2<-data.frame(cluster,structure,serial.number)

thankyou

Sam Mwenda
  • 150
  • 8

3 Answers3

2

Using dplyr

library(dplyr)
df %>%
      group_by(cluster, structure) %>%
      mutate(serial.number = row_number())

or with data.table

library(data.table)
setDT(df)[, serial.number := rowid(cluster, structure)]
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Try ave + seq_along if you are with base R

within(str, serial.number <- ave(structure,cluster,FUN = seq_along))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

I guess you are looking for this:

library(data.table)
df <- setDT(str)
df[, serial.number := seq(.N), by = .(cluster,structure)] 
peter
  • 756
  • 5
  • 16