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

问题描述

我有一个包含 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

使用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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...