1

if petal. width is o.1 then my new column value should be 1 :n number like wise it has ti print for every unique values

`data=iris
data=data[order(data$Petal.Width),]
count=1
data$iden<-with(data,(data$Petal.Width))
for (i in 1:length(data)) {
if(data$Petal.Width[i] == data$Petal.Width[i]){
 count=count+1 }}`

1 Answers1

0

We can create the sequence with ave from base R

data$Seq <- with(data, ave(seq_along(Petal.Width), Petal.Width, FUN = seq_along))

Or using data.table

library(data.table)
data$Seq <- rowid(data$Petal.Width)

Or with dplyr

library(dplyr)
data %>%
     group_by(Petal.Width) %>%
     mutate(Seq = row_number())
akrun
  • 874,273
  • 37
  • 540
  • 662