从 R 数据框中的观察值生成新变量

时间:2021-03-29 18:48:32

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

1 个答案:

答案 0 :(得分:2)

从这个子集开始:

> 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

使用pivot_wider

library(tidyverse)    
data_wide <- data %>% pivot_wider(names_from = event_type, values_from = n)

哪个返回:

> 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
相关问题