2

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
  • 140
  • 7

3 Answers3

1

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
  • 789,025
  • 32
  • 460
  • 575
0

Try ave + seq_along if you are with base R

within(str, serial.number <- ave(structure,cluster,FUN = seq_along))
ThomasIsCoding
  • 80,151
  • 7
  • 17
  • 65
0

I guess you are looking for this:

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