如何在R中进行列拆分和广播

时间:2018-07-02 09:51:04

标签: r

我在r中有以下数据框

  Date            Weather
  2018-01-01      Rain,Fog
  2018-01-02      Fog,Rain
  2018-01-03      Rain
  2018-01-04      Thunderstorm
  2018-01-05      Rain,Fog
  2018-01-06      Rain,Thunderstorm

我想要的数据框是

  Date         Rain     Fog      Thunderstorm
  2018-01-01    1        1           0
  2018-01-02    1        1           0
  2018-01-03    1        0           0
  2018-01-04    0        0           1
  2018-01-05    1        1           0
  2018-01-06    1        0           1

如何在R中做到这一点?

2 个答案:

答案 0 :(得分:1)

这里有tidyverse的可能性

library(tidyverse)
df %>%
    separate_rows(Weather) %>%
    group_by(Date) %>%
    mutate(n = 1) %>%
    spread(Weather, n, fill = 0)
## A tibble: 6 x 4
## Groups:   Date [6]
#  Date         Fog  Rain Thunderstorm
#  <fct>      <dbl> <dbl>        <dbl>
#1 2018-01-01    1.    1.           0.
#2 2018-01-02    1.    1.           0.
#3 2018-01-03    0.    1.           0.
#4 2018-01-04    0.    0.           1.
#5 2018-01-05    1.    1.           0.
#6 2018-01-06    0.    1.           1.

答案 1 :(得分:1)

splitstackshape包对于此类任务非常有用,这里我们使用函数cSplit_e

library(splitstackshape)
cSplit_e(data = df, 
         split.col = "Weather",
         sep = ",",
         mode = "binary",
         type = "character",
         drop = TRUE,
         fill = 0)
#        Date Weather_Fog Weather_Rain Weather_Thunderstorm
#1 2018-01-01           1            1                    0
#2 2018-01-02           1            1                    0
#3 2018-01-03           0            1                    0
#4 2018-01-04           0            0                    1
#5 2018-01-05           1            1                    0
#6 2018-01-06           0            1                    1

数据

df <- structure(list(Date = c("2018-01-01", "2018-01-02", "2018-01-03", 
"2018-01-04", "2018-01-05", "2018-01-06"), Weather = c("Rain,Fog", 
"Fog,Rain", "Rain", "Thunderstorm", "Rain,Fog", "Rain,Thunderstorm"
)), .Names = c("Date", "Weather"), class = "data.frame", row.names = c(NA, 
-6L))