从R中数据帧中的观测值生成新变量

从R中数据帧中的观测值生成新变量,r,mutate,R,Mutate,我有一个包含4个变量的数据集,月份、国家、事件类型和n,如下所示,事件类型包括6个不同的因素。我想将它们转换为变量作为新列,每个变量都应该包含n。非常感谢您的指导 month country event_type n Apr-2018 Afghanistan Battles 1648 Apr-2018 Afghanistan Explosions/Remote violence 683 Apr-2018 Afghanistan Protests 3

我有一个包含4个变量的数据集,月份、国家、事件类型和n,如下所示,事件类型包括6个不同的因素。我想将它们转换为变量作为新列,每个变量都应该包含n。非常感谢您的指导

month       country     event_type  n
Apr-2018    Afghanistan Battles 1648
Apr-2018    Afghanistan Explosions/Remote violence  683
Apr-2018    Afghanistan Protests    30
Apr-2018    Afghanistan Riots   2
Apr-2018    Afghanistan Strategic developments  31
Apr-2018    Afghanistan Violence against civilians  44
Apr-2018    Colombia    Battles 90
Apr-2018    Colombia    Explosions/Remote violence  20
Apr-2018    Colombia    Protests    7
Apr-2018    Colombia    Strategic developments  64
Apr-2018    Colombia    Violence against civilians  148
Apr-2018    India   Battles 152
Apr-2018    India   Explosions/Remote violence  50
Apr-2018    India   Protests    1347
Apr-2018    India   Riots   592
Apr-2018    India   Strategic developments  18
比如,

Month     Country     Battle Explosions..  Protests Riots Strategic development
Apr-2018  Afghanistan 1648   683           30       2     31

从这个子集开始:

> print(data)
# A tibble: 6 x 4
  month    country     event_type                     n
  <chr>    <chr>       <chr>                      <dbl>
1 Apr-2018 Afghanistan Battles                     1648
2 Apr-2018 Afghanistan Explosions/Remote violence   683
3 Apr-2018 Afghanistan Protests                      30
4 Apr-2018 Afghanistan Riots                          2
5 Apr-2018 Afghanistan Strategic developments        31
6 Apr-2018 Afghanistan Violence against civilians    44
返回:

> print(data_wide)
# A tibble: 1 x 8
  month    country     Battles `Explosions/Remote violence` Protests Riots `Strategic developments` `Violence against civilians`
  <chr>    <chr>         <dbl>                        <dbl>    <dbl> <dbl>                    <dbl>                        <dbl>
1 Apr-2018 Afghanistan    1648                          683       30     2                       31                           44
>打印(数据范围)
#一个tibble:1 x 8
月乡村战斗`爆炸/远程暴力`抗议骚乱`战略发展`对平民的暴力`
2018年4月1日阿富汗1648 683 30 2 31 44

从这个子集开始:

> print(data)
# A tibble: 6 x 4
  month    country     event_type                     n
  <chr>    <chr>       <chr>                      <dbl>
1 Apr-2018 Afghanistan Battles                     1648
2 Apr-2018 Afghanistan Explosions/Remote violence   683
3 Apr-2018 Afghanistan Protests                      30
4 Apr-2018 Afghanistan Riots                          2
5 Apr-2018 Afghanistan Strategic developments        31
6 Apr-2018 Afghanistan Violence against civilians    44
返回:

> print(data_wide)
# A tibble: 1 x 8
  month    country     Battles `Explosions/Remote violence` Protests Riots `Strategic developments` `Violence against civilians`
  <chr>    <chr>         <dbl>                        <dbl>    <dbl> <dbl>                    <dbl>                        <dbl>
1 Apr-2018 Afghanistan    1648                          683       30     2                       31                           44
>打印(数据范围)
#一个tibble:1 x 8
月乡村战斗`爆炸/远程暴力`抗议骚乱`战略发展`对平民的暴力`
2018年4月1日阿富汗1648 683 30 2 31 44

非常感谢,这很有效。此外,如何从月份数据创建新变量。我想把柱分为月和年@ornaldo_@ÜmitSeven
单独(月,c(“月”,“年”),sep=“-”)
。不客气。非常感谢,这很有效。此外,如何从月份数据创建新变量。我想把柱分为月和年@ornaldo_@ÜmitSeven
单独(月,c(“月”,“年”),sep=“-”)
。不客气。