goegges
goegges

Reputation: 107

Generate random numbers for groups R

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

Answers (3)

zx8754
zx8754

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

Ronak Shah
Ronak Shah

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

chinsoon12
chinsoon12

Reputation: 25225

An option in data.table:

setDT(data)[, random := sample(100, 1), ID]

Upvotes: 1

Related Questions