基于R中每组中另一个变量首次出现的新变量

基于R中每组中另一个变量首次出现的新变量,r,datatable,data-manipulation,R,Datatable,Data Manipulation,我有一个像这样的长数据帧: set.seed(17) players<-rep(1:2, c(5,5)) decs<-sample(1:3,10,replace=TRUE) world<-sample(1:2,10,replace=TRUE) gamematrix<-cbind(players,decs,world) gamematrix<-data.frame(gamematrix) gamematrix players decs world 1

我有一个像这样的长数据帧:

set.seed(17)
players<-rep(1:2, c(5,5))
decs<-sample(1:3,10,replace=TRUE)
world<-sample(1:2,10,replace=TRUE)
gamematrix<-cbind(players,decs,world)
gamematrix<-data.frame(gamematrix)
gamematrix

     players decs world
1        1    1     1
2        1    3     1
3        1    2     2
4        1    3     2
5        1    2     2
6        2    2     2
7        2    1     2
8        2    1     1
9        2    3     2
10       2    1     2

有什么办法吗

这种
tidyverse
方法可能有点麻烦,但它应该能满足您的需求

library(tidyverse)
left_join(
  gamematrix,
  gamematrix %>%
    filter(decs == 3) %>%
    group_by(players) %>%
    slice(1) %>%
    mutate(player_type = ifelse(world == 1, 6, 7)) %>%
    select(players, player_type),
  by = 'players'
)
#   players decs world player_type
#1        1    1     1           6
#2        1    3     1           6
#3        1    2     2           6
#4        1    3     2           6
#5        1    2     2           6
#6        2    2     2           7
#7        2    1     2           7
#8        2    1     1           7
#9        2    3     2           7
#10       2    1     2           7

其思想是
过滤
您的数据,以便观察
decs==3
,提取每个“玩家”的第一个元素,根据“世界”状态添加
player\u type
,最后与您的原始数据合并

这种
tidyverse
方法可能有点麻烦,但它应该能满足您的需求

library(tidyverse)
left_join(
  gamematrix,
  gamematrix %>%
    filter(decs == 3) %>%
    group_by(players) %>%
    slice(1) %>%
    mutate(player_type = ifelse(world == 1, 6, 7)) %>%
    select(players, player_type),
  by = 'players'
)
#   players decs world player_type
#1        1    1     1           6
#2        1    3     1           6
#3        1    2     2           6
#4        1    3     2           6
#5        1    2     2           6
#6        2    2     2           7
#7        2    1     2           7
#8        2    1     1           7
#9        2    3     2           7
#10       2    1     2           7

其思想是
过滤
您的数据,以便观察
decs==3
,提取每个“玩家”的第一个元素,根据“世界”状态添加
player\u type
,最后与您的原始数据合并

一个选项是使用
cumsum(decs==3)==1
为玩家查找第一次出现的
decs==3
。现在,
dplyr::case_when
可以用来分配播放器类型

library(dplyr)

gamematrix %>% group_by(players) %>%
mutate(player_type = case_when(
   world[first(which(cumsum(decs==3)==1))] == 1 ~ 6L,
   world[first(which(cumsum(decs==3)==1))] == 2 ~ 7L,
  TRUE                              ~ NA_integer_))


# # A tibble: 10 x 4
# # Groups: players [2]
#   players  decs world player_type
#     <int> <int> <int>       <int>
# 1       1     1     1           6
# 2       1     3     1           6
# 3       1     2     2           6
# 4       1     3     2           6
# 5       1     2     2           6
# 6       2     2     2           7
# 7       2     1     2           7
# 8       2     1     1           7
# 9       2     3     2           7
# 10       2     1     2           7  
库(dplyr)
gamematrix%%>%组由(玩家)%%>%
变异(玩家类型=案例)(
世界[第一(其中(cumsum(decs==3)==1))]==1~6升,
世界[first(其中(cumsum(decs==3)==1))]==2~7L,
TRUE~NA_整数)
##tibble:10 x 4
##团体:玩家[2]
#球员decs世界球员类型
#              
# 1       1     1     1           6
# 2       1     3     1           6
# 3       1     2     2           6
# 4       1     3     2           6
# 5       1     2     2           6
# 6       2     2     2           7
# 7       2     1     2           7
# 8       2     1     1           7
# 9       2     3     2           7
# 10       2     1     2           7  

一个选项是使用
cumsum(decs==3)==1
为玩家查找第一次出现的
decs==3
。现在,
dplyr::case_when
可以用来分配播放器类型

library(dplyr)

gamematrix %>% group_by(players) %>%
mutate(player_type = case_when(
   world[first(which(cumsum(decs==3)==1))] == 1 ~ 6L,
   world[first(which(cumsum(decs==3)==1))] == 2 ~ 7L,
  TRUE                              ~ NA_integer_))


# # A tibble: 10 x 4
# # Groups: players [2]
#   players  decs world player_type
#     <int> <int> <int>       <int>
# 1       1     1     1           6
# 2       1     3     1           6
# 3       1     2     2           6
# 4       1     3     2           6
# 5       1     2     2           6
# 6       2     2     2           7
# 7       2     1     2           7
# 8       2     1     1           7
# 9       2     3     2           7
# 10       2     1     2           7  
库(dplyr)
gamematrix%%>%组由(玩家)%%>%
变异(玩家类型=案例)(
世界[第一(其中(cumsum(decs==3)==1))]==1~6升,
世界[first(其中(cumsum(decs==3)==1))]==2~7L,
TRUE~NA_整数)
##tibble:10 x 4
##团体:玩家[2]
#球员decs世界球员类型
#              
# 1       1     1     1           6
# 2       1     3     1           6
# 3       1     2     2           6
# 4       1     3     2           6
# 5       1     2     2           6
# 6       2     2     2           7
# 7       2     1     2           7
# 8       2     1     1           7
# 9       2     3     2           7
# 10       2     1     2           7  

我们可以使用
数据表

library(data.table)
setDT(gamematrix)[, player_type := c(7, 6)[any(decs == 3& world == 1) + 1],
              by =  players]
gamematrix
#    players decs world player_type
# 1:       1    1     1           6
# 2:       1    3     1           6
# 3:       1    2     2           6
# 4:       1    3     2           6
# 5:       1    2     2           6
# 6:       2    2     2           7
# 7:       2    1     2           7
# 8:       2    1     1           7
# 9:       2    3     2           7
#10:       2    1     2           7

我们可以使用
data.table

library(data.table)
setDT(gamematrix)[, player_type := c(7, 6)[any(decs == 3& world == 1) + 1],
              by =  players]
gamematrix
#    players decs world player_type
# 1:       1    1     1           6
# 2:       1    3     1           6
# 3:       1    2     2           6
# 4:       1    3     2           6
# 5:       1    2     2           6
# 6:       2    2     2           7
# 7:       2    1     2           7
# 8:       2    1     1           7
# 9:       2    3     2           7
#10:       2    1     2           7

我不确定我是否理解您的预期输出
player_type
似乎只是
players+5
,而不管“decs”的任何“首次出现”,不是吗?我不确定我是否理解您的预期输出
player_type
似乎只是
players+5
,不管“decs”首次出现,不是吗?@YefR很高兴它奏效了。如果他们解决了你的问题,请考虑接受任何答案。Regards@YefR很高兴它起作用了。如果他们解决了你的问题,请考虑接受任何答案。当做