Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

r - Randomly pick a value from a set of rows and add value to new row below

my R skills are not sufficient to tackle this issue so I'm hoping someone can help.

My data look like this:

head(human.players,25)
Season Episode Round Player Player_type Crowd_size q1_a q2_a q3_a q4_a q5_a
2020 1 1 1 1 3 0 1 0 0 NA
2020 1 1 2 1 3 0 1 1 1 NA
2020 1 1 3 1 3 0 0 0 1 NA
2020 1 2 1 1 3 1 1 0 1 NA
2020 1 2 2 1 3 1 0 1 0 NA
2020 1 2 3 1 3 1 1 1 0 NA
2020 1 3 1 1 3 0 1 0 0 NA
2020 1 3 2 1 3 0 1 1 1 NA
2020 1 3 3 1 3 0 0 1 1 NA
2020 1 4 1 1 3 0 0 1 1 NA
2020 1 4 2 1 3 0 0 1 1 NA
2020 1 4 3 1 3 0 0 1 1 NA
2020 1 5 1 1 2 1 1 0 0 NA
2020 1 5 2 1 2 1 1 1 0 NA
2020 1 5 3 1 2 NA NA NA NA NA
2020 1 6 1 1 2 0 0 0 0 NA
2020 1 6 2 1 2 0 0 0 0 NA
2020 1 6 3 1 2 NA NA NA NA NA
2020 1 7 1 1 2 0 1 1 1 NA
2020 1 7 2 1 2 1 0 0 1 NA
2020 1 7 3 1 2 NA NA NA NA NA
2020 2 1 1 1 3 1 1 0 0 NA
2020 2 1 2 1 3 0 0 0 1 NA
2020 2 1 3 1 3 0 1 1 0 NA
question from:https://stackoverflow.com/questions/66046804/randomly-pick-a-value-from-a-set-of-rows-and-add-value-to-new-row-below

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's one pipe that samples the new players and their scores into a separate frame, which you can then bind_rows back into the original data.

set.seed(2021)
newplayers <- dat %>%
  filter(!is.na(q1_a)) %>%
  group_by(Season, Episode, Round) %>%
  summarize(across(everything(), ~ sample(., size=1)), .groups = "drop") %>%
  mutate(Player = NA_integer_, Player_type = NA_integer_)
newplayers
# # A tibble: 8 x 11
#   Season Episode Round Player Player_type Crowd_size  q1_a  q2_a  q3_a  q4_a q5_a 
#    <int>   <int> <int>  <int>       <int>      <int> <int> <int> <int> <int> <lgl>
# 1   2020       1     1     NA          NA          3     0     0     1     1 NA   
# 2   2020       1     2     NA          NA          3     1     1     0     0 NA   
# 3   2020       1     3     NA          NA          3     0     1     1     0 NA   
# 4   2020       1     4     NA          NA          3     0     0     1     1 NA   
# 5   2020       1     5     NA          NA          2     1     1     1     0 NA   
# 6   2020       1     6     NA          NA          2     0     0     0     0 NA   
# 7   2020       1     7     NA          NA          2     0     0     1     1 NA   
# 8   2020       2     1     NA          NA          3     0     1     0     0 NA   

bind_rows(dat, newplayers) %>%
  arrange(Season, Episode, Round, is.na(Player), Player) %>%
  head(.)
#   Season Episode Round Player Player_type Crowd_size q1_a q2_a q3_a q4_a q5_a
# 1   2020       1     1      1           1          3    0    1    0    0   NA
# 2   2020       1     1      2           1          3    0    1    1    1   NA
# 3   2020       1     1      3           1          3    0    0    0    1   NA
# 4   2020       1     1     NA          NA          3    0    0    1    1   NA
# 5   2020       1     2      1           1          3    1    1    0    1   NA
# 6   2020       1     2      2           1          3    1    0    1    0   NA

I didn't know what values to put into Player*, so I chose NA.


Data

# dput(dat)
dat <- structure(list(Season = c(2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L), Episode = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), Round = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L), Player = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Player_type = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Crowd_size = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), q1_a = c(0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, NA, 0L, 0L, NA, 0L, 1L, NA, 1L, 0L, 0L), q2_a = c(1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, NA, 0L, 0L, NA, 1L, 0L, NA, 1L, 0L, 1L), q3_a = c(0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, NA, 0L, 0L, NA, 1L, 0L, NA, 0L, 0L, 1L), q4_a = c(0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, NA, 0L, 0L, NA, 1L, 1L, NA, 0L, 1L, 0L), q5_a = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -24L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...