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

时间:2018-06-24 09:18:08

标签: 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        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

我想为每个播放器创建一个新变量,该变量基于decs == 3变量的 first 外观和世界状况。

也就是说,如果当第一次出现“ decs”时,世界的状态是“ 1”,那么新变量应获得值“ 6”,否则为“ 7”,如下所示:

    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

任何想法该怎么做?

3 个答案:

答案 0 :(得分: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

想法是filter为观测数据,其中decs == 3,提取每个“玩家”的第一个元素,添加player_type,以符合“世界”的状态,最后合并与您的原始数据。

答案 1 :(得分:1)

一种选择是使用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  

答案 2 :(得分:0)

我们可以使用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