Reputation: 77
I have this problem below to address. Your inputs/pointers are very much appreciated.Thanks in advance!
I have a data.table called Defect. And the first column is Length
Defect Table :
Length
6355
6236
1345
3123
I want to add two more columns called DefectStarts and DefectEnds. I would like to use random function to determine where the defect starts and where it ends.
Basic criteria is as follows:
DefectStarts and DefectEnds <= Length
DefectEnds > DefectStarts
DefectEnds - DefectStarts <= Length
Final data should something like this(values could be different as long as it satisfies above conditions)
Defect table:
Length DefectStarts DefectEnds
6355 1234 4356
6236 2000 4567
1345 500 689
3123 342 3120
Upvotes: 1
Views: 1524
Reputation: 3492
Psuedocode:
i for (1 to length(df){
df[i,DefectStarts]=sample(df[i,Length],1,T)
df[i,DefectEnds]=sample(df[i,Length]-df[i,DefectStarts],1,T)
}
Upvotes: 0
Reputation: 57686
runif(n, min, max)
generates n
random numbers with a range from min
to max
. The latter two arguments can be vectors.
n <- nrow(df)
df$start <- runif(n, max=df$Length)
df$end <- runif(n, min=df$start, max=df$Length)
Using the dplyr package:
df <- mutate(df, start=runif(n(), max=Length), end=runif(n(), min=start, max=Length))
Upvotes: 3
Reputation: 77
After some messing around with the code, this seemed to do the job :
Defect <- Defect[,DefectStarts:=runif(row_count,min=1,max=Defect$Length)]
Defect <- Defect[,DefectEnds:=runif(row_count,min=Defect$DefectStarts,max=Defect$Length)]
Thanks again for your support! Much appreciated. And please do suggest if there are any better ways to do this.
Upvotes: 0
Reputation: 1795
Consider df
to be your dataframe:
df$random<-runif(nrow(df)) #just create a random column and fill it with random numbers
df$random<-ifelse(df$DefectEnds > df$DefectStarts,df$random,NA)#select rows with this condition
Upvotes: 0