SiH
SiH

Reputation: 1546

create desired tibble without using loop

I have n segmentation (0 to n-1) in a data. I know percentage of male in each segment.

How do I write a dynamic code to create arrays of segmentation and males without using loops?

For e.g. -

# datasize = 20, # segmentations = 2
N = 20
percent_each_segmentation = c(0.4, 0.6)
percent_male_per_segmentation = c(0.5, 0.25)

The desired output would look like

segmentation <- c(rep(0, 8), rep(1, 12))
# segmentation 0 - 0.4*20, segmentation 1 - 0.6*20

male <- c(rep(1, 4), rep(0, 4), rep(1, 3), rep(0, 9))
# male in segmentation 0 : 0.5*0.4*20, segmentation 1 : 0.25*0.6*20

tbl <- tibble(segmentation, male)

Upvotes: 1

Views: 36

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

You could use the following :

N = 20
perc_seg = c(0.4, 0.6)
perc_male = c(0.5, 0.25)
n <- N * perc_seg
n_male <- N * perc_seg * perc_male
n_female <- n - n_male

data.frame(segmentation = rep(seq_along(perc_seg) - 1, n),
           male = rep(rep(c(1, 0), length(perc_seg)),c(rbind(n_male, n_female))))

#   segmentation male
#1             0    1
#2             0    1
#3             0    1
#4             0    1
#5             0    0
#6             0    0
#7             0    0
#8             0    0
#9             1    1
#10            1    1
#11            1    1
#12            1    0
#13            1    0
#14            1    0
#15            1    0
#16            1    0
#17            1    0
#18            1    0
#19            1    0
#20            1    0

Upvotes: 1

akrun
akrun

Reputation: 886948

We could use the rep as

N <- 20
r1 <- percent_each_segmentation * 
      percent_male_per_segmentation * N
bnry <- c(1, 0)
r2 <- rep(r1, each = length(bnry))

r2[length(r2)] <- N - sum(head(r2, -1))


rep(rep(bnry, length.out = length(r2)), r2)

For the segmentation

rep(c(0, 1), N * percent_each_segmentation)

Upvotes: 2

Related Questions