将两列共享相同的值与第三列组合

时间:2017-08-09 12:10:30

标签: r dataframe structure

我正在以一种我可以利用它的方式尝试构建我的情绪列表。

我有一个数据框,结构如下:

df <- data.frame("vehicle" = c("car", "plane", "bicycle", "helicopter", "train"),
                 "Value" = c(0.5, 0.2, 0.1, -1, -0.7),
                 "type" = c("toyota, mercedes, whatever", "Airbus, Boeing", 
                            "Mountain bike", "NA", "TGV, ICE"))

我希望&#34;键入&#34;将他们的价值观添加到&#34;车辆&#34;列,以获取这样的数据框:

dfdesired <- data.frame("vehicle" = c("car", "toyota", "mercedes", "whatever", "plane", "Airbus", 
                                      "Boeing", "bicycle", "Mountain bike", "helicopter", "train", "TGV", "ICE"),
                        "Value" =c(0.5, 0.5, 0.5, 0.5, 0.2, 0.2, 0.2, 0.1, 0.1, -1, -0.7, -0.7, -0.7))

到目前为止有没有人遇到过类似的问题?

非常感谢!

1 个答案:

答案 0 :(得分:6)

使用tidyr你可以这样做:

library(dplyr)
library(tidyr)
df  %>% mutate (type2 = strsplit(paste0(vehicle,", ",type),", ")) %>%
  unnest(type2) %>%
  select(vehicle = type2, Value)

         vehicle Value
1            car   0.5
2         toyota   0.5
3       mercedes   0.5
4       whatever   0.5
5          plane   0.2
6         Airbus   0.2
7         Boeing   0.2
8        bicycle   0.1
9  Mountain bike   0.1
10    helicopter  -1.0
11         train  -0.7
12           TGV  -0.7
13           ICE  -0.7

首先,我们将vehicle添加到type字符串,然后我们将字符串拆分为list。此时我们有data.frame包含一列列表,我们使用unnest按列表项获取一行,然后选择我们想要的列。