Reputation: 107
I want to generate the same random number for groups. Unfortunately, my following code generates random number for each row.
randomised <- data %>%
group_by(`ID`)%>%
mutate(random = sample(1:100,n(), replace = TRUE))
Any help is appreciated.
Upvotes: 4
Views: 2766
Reputation: 56119
Not sure why we need this, if we are trying to anonymise the IDs (cyl column in mtcars example data), then this is pretty random to me:
library(dplyr)
mtcars %>%
mutate(random = as.integer(as.factor(cyl)))
Upvotes: 1
Reputation: 388897
You should just select 1 value from sample
which will be recycled for all the values in the group.
library(dplyr)
data %>% group_by(ID)%>% mutate(random = sample(100,1))
Or in base R :
data$random <- with(data, ave(seq_along(ID), ID,FUN = function(x) sample(100, 1)))
Upvotes: 6
Reputation: 25225
An option in data.table
:
setDT(data)[, random := sample(100, 1), ID]
Upvotes: 1