如何每年为独特的球员提供一个数字
How to give every year a number for unique players
是否可以选择每年为 R 中的唯一玩家提供一个编号?
示例:
playerID yearID stint teamID lgID POS G PO A E DP score
<chr> <chr> <chr> <fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 aardsda01 2004 1 SFN NL P 11 0 0 0 0 0.0
2 aardsda01 2006 1 CHN NL P 45 1 5 0 1 7.5
3 aardsda01 2007 1 CHA AL P 25 2 4 1 0 3.5
4 aardsda01 2008 1 BOS AL P 47 3 6 0 0 9.0
5 aardsda01 2009 1 SEA AL P 73 2 5 0 1 7.5
6 aardsda01 2010 1 SEA AL P 53 2 3 1 0 2.0
7 aardsda01 2012 1 NYA AL P 1 0 0 0 0 0.0
8 aardsda01 2013 1 NYN NL P 43 1 5 0 0 7.5
9 aardsda01 2015 1 ATL NL P 33 0 1 1 0 -1.0
10 aaronha01 1954 1 ML1 NL LF 105 205 4 6 0 -9.0
playerID yearID stint teamID lgID POS G PO A E DP score value
<chr> <chr> <chr> <fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 aardsda01 2004 1 SFN NL P 11 0 0 0 0 0.0 1
2 aardsda01 2006 1 CHN NL P 45 1 5 0 1 7.5 2
3 aardsda01 2007 1 CHA AL P 25 2 4 1 0 3.5 3
4 aardsda01 2008 1 BOS AL P 47 3 6 0 0 9.0 4
5 aardsda01 2009 1 SEA AL P 73 2 5 0 1 7.5 5
6 aardsda01 2010 1 SEA AL P 53 2 3 1 0 2.0 6
7 aardsda01 2012 1 NYA AL P 1 0 0 0 0 0.0 7
8 aardsda01 2013 1 NYN NL P 43 1 5 0 0 7.5 8
9 aardsda01 2015 1 ATL NL P 33 0 1 1 0 -1.0 9
10 aaronha01 1954 1 ML1 NL LF 105 205 4 6 0 -9.0 1
value
栏是我想要的。
R 中有这样做的选项吗???
谢谢,
尼克
使用 dplyr
您可以:
df %>% group_by(playerID) %>% mutate(value=1:n()) %>% ungroup
这个小管子:
- 根据
playerID
对您的 data.frame
(假设它被命名为 df
)进行分组
- 然后创建一个额外的列(此处命名为
value
,但您可以更改它)填充整数(非常需要,因为它们是每个组中的行)
- 完成后取消分组
data.frame
。
这就是你要找的吗?
是否可以选择每年为 R 中的唯一玩家提供一个编号? 示例:
playerID yearID stint teamID lgID POS G PO A E DP score
<chr> <chr> <chr> <fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 aardsda01 2004 1 SFN NL P 11 0 0 0 0 0.0
2 aardsda01 2006 1 CHN NL P 45 1 5 0 1 7.5
3 aardsda01 2007 1 CHA AL P 25 2 4 1 0 3.5
4 aardsda01 2008 1 BOS AL P 47 3 6 0 0 9.0
5 aardsda01 2009 1 SEA AL P 73 2 5 0 1 7.5
6 aardsda01 2010 1 SEA AL P 53 2 3 1 0 2.0
7 aardsda01 2012 1 NYA AL P 1 0 0 0 0 0.0
8 aardsda01 2013 1 NYN NL P 43 1 5 0 0 7.5
9 aardsda01 2015 1 ATL NL P 33 0 1 1 0 -1.0
10 aaronha01 1954 1 ML1 NL LF 105 205 4 6 0 -9.0
playerID yearID stint teamID lgID POS G PO A E DP score value
<chr> <chr> <chr> <fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 aardsda01 2004 1 SFN NL P 11 0 0 0 0 0.0 1
2 aardsda01 2006 1 CHN NL P 45 1 5 0 1 7.5 2
3 aardsda01 2007 1 CHA AL P 25 2 4 1 0 3.5 3
4 aardsda01 2008 1 BOS AL P 47 3 6 0 0 9.0 4
5 aardsda01 2009 1 SEA AL P 73 2 5 0 1 7.5 5
6 aardsda01 2010 1 SEA AL P 53 2 3 1 0 2.0 6
7 aardsda01 2012 1 NYA AL P 1 0 0 0 0 0.0 7
8 aardsda01 2013 1 NYN NL P 43 1 5 0 0 7.5 8
9 aardsda01 2015 1 ATL NL P 33 0 1 1 0 -1.0 9
10 aaronha01 1954 1 ML1 NL LF 105 205 4 6 0 -9.0 1
value
栏是我想要的。
R 中有这样做的选项吗???
谢谢,
尼克
使用 dplyr
您可以:
df %>% group_by(playerID) %>% mutate(value=1:n()) %>% ungroup
这个小管子:
- 根据
playerID
对您的 - 然后创建一个额外的列(此处命名为
value
,但您可以更改它)填充整数(非常需要,因为它们是每个组中的行) - 完成后取消分组
data.frame
。
data.frame
(假设它被命名为 df
)进行分组
这就是你要找的吗?