谁能帮我用 R 处理数据? [图片更新]
Could anyone help me with the data processing with R? [Picture Updated]
大家早上好!我是 R 编程的新手。最近,我遇到了一些问题。我想处理附件中的图片等数据。一列有两种数据excelfile/table共有9000+行和几列,而有两种数据的列只是其中一列[=14] =]
originaldata
ID Fruit/FruitJuice
<dbl> <chr>
1 1 NA/applejuice:15(ml)
2 2 banana:10(kg)/orangejuice:20(ml);tomatojuice:25(ml)
3 3 watermelon:10(kg)/NA
4 4 banana:5(kg);grape:6(kg)/orangejuice:30(ml);applejuice:50(ml);mangojuice:25(ml)
简单描述一下数据,2种数据的栏目分别是水果和果汁,用“/”隔开,括号内有单位,而水果或果汁中的对象用“/”隔开";".
由于实际数据框包含的列比我显示的图像多,我在网上做了一些搜索,但我仍然不知道如何解决它并希望得到最终的 table 如下所示,有人可以帮我吗?谢谢
Final data wish-to-be
ID `Fruit/FruitJuice`
1 1 Fruit_NA
2 1 Fruitjuice_applejuice:15(ml)
3 2 Fruit_banana:10(kg)
4 2 Fruitjuice_orangejuice:20(ml)
5 2 Fruitjuice_tomatojuice:25(ml)
6 3 Fruit_watermelon:10(kg)
7 3 Fruitjuice_NA
8 4 Fruit_banana:5(kg)
9 4 Fruit_grape:6(kg)
10 4 Fruitjuice_orangejuice:30(ml)
11 4 Fruitjuice_applejuice:50(ml)
12 4 Fruitjuice_mangojuice:25(ml)
这是 tidyr
的方法:
library(tidyverse)
data %>%
separate(2,into = c("Fruit","FruitJuice"), sep = "/") %>%
pivot_longer(-ID) %>% separate_rows(value, sep = ";") %>%
summarise(ID = ID, `Fruit/FruitJuice` = str_c(name,"_",value))
# A tibble: 12 x 2
ID `Fruit/FruitJuice`
<int> <chr>
1 1 Fruit_NA
2 1 FruitJuice_applejuice:15(ml)
3 2 Fruit_banana:10(kg)
4 2 FruitJuice_orangejuice:20(ml)
5 2 FruitJuice_tomatojuice:25(ml)
6 3 Fruit_watermelon:10(kg)
7 3 FruitJuice_NA
8 4 Fruit_banana:5(kg)
9 4 Fruit_grape:6(kg)
10 4 FruitJuice_orangejuice:30(ml)
11 4 FruitJuice_applejuice:50(ml)
12 4 FruitJuice_mangojuice:25(ml)
大家早上好!我是 R 编程的新手。最近,我遇到了一些问题。我想处理附件中的图片等数据。一列有两种数据excelfile/table共有9000+行和几列,而有两种数据的列只是其中一列[=14] =]
originaldata
ID Fruit/FruitJuice
<dbl> <chr>
1 1 NA/applejuice:15(ml)
2 2 banana:10(kg)/orangejuice:20(ml);tomatojuice:25(ml)
3 3 watermelon:10(kg)/NA
4 4 banana:5(kg);grape:6(kg)/orangejuice:30(ml);applejuice:50(ml);mangojuice:25(ml)
简单描述一下数据,2种数据的栏目分别是水果和果汁,用“/”隔开,括号内有单位,而水果或果汁中的对象用“/”隔开";".
由于实际数据框包含的列比我显示的图像多,我在网上做了一些搜索,但我仍然不知道如何解决它并希望得到最终的 table 如下所示,有人可以帮我吗?谢谢
Final data wish-to-be
ID `Fruit/FruitJuice`
1 1 Fruit_NA
2 1 Fruitjuice_applejuice:15(ml)
3 2 Fruit_banana:10(kg)
4 2 Fruitjuice_orangejuice:20(ml)
5 2 Fruitjuice_tomatojuice:25(ml)
6 3 Fruit_watermelon:10(kg)
7 3 Fruitjuice_NA
8 4 Fruit_banana:5(kg)
9 4 Fruit_grape:6(kg)
10 4 Fruitjuice_orangejuice:30(ml)
11 4 Fruitjuice_applejuice:50(ml)
12 4 Fruitjuice_mangojuice:25(ml)
这是 tidyr
的方法:
library(tidyverse)
data %>%
separate(2,into = c("Fruit","FruitJuice"), sep = "/") %>%
pivot_longer(-ID) %>% separate_rows(value, sep = ";") %>%
summarise(ID = ID, `Fruit/FruitJuice` = str_c(name,"_",value))
# A tibble: 12 x 2
ID `Fruit/FruitJuice`
<int> <chr>
1 1 Fruit_NA
2 1 FruitJuice_applejuice:15(ml)
3 2 Fruit_banana:10(kg)
4 2 FruitJuice_orangejuice:20(ml)
5 2 FruitJuice_tomatojuice:25(ml)
6 3 Fruit_watermelon:10(kg)
7 3 FruitJuice_NA
8 4 Fruit_banana:5(kg)
9 4 Fruit_grape:6(kg)
10 4 FruitJuice_orangejuice:30(ml)
11 4 FruitJuice_applejuice:50(ml)
12 4 FruitJuice_mangojuice:25(ml)