我有一个很大的数据框,其中包含一个在线商店的产品,几种产品以不同的方式记录在其中,如下所示:
1: milk 1-liter low, fat
2: M I L K 1-liter L ow fat
3: Milk. 1_liter LOW FAT
4: Milk 1_liter L F A T
5: MILK 1.5_liter Hi gh FAT
我需要将其拆分
我希望从我的数据中得到这个信息
V1 v2 v3 v4
milk 1-liter low fat
MILK 1-liter Low fat
Milk. 1_liter LOW FAT
Milk 1_liter L FAT
MILK 1.5_liter High FAT
答案 0 :(得分:0)
我很想知道别人是否有一个更自动化的解决方案,因为我经常会有类似的混乱数据。
我知道如何执行此操作的唯一方法是编写一堆正则表达式(通过stringr::str_replace()
)以协调数据帧中的行。您可以使用tidyr::separate()
将product
列分成多个列:
library(stringr)
library(dplyr)
library(tidyr)
dat <- tibble(product = c("milk 1-liter low, fat",
"M I L K 1-liter L ow fat",
"Milk. 1_liter LOW FAT",
"Milk 1_liter L F A T",
"MILK 1.5_liter Hi gh FAT"))
dat %>%
mutate(product = str_replace(product, "(milk|MILK|Milk|M I L K|Milk)\\.*", "milk"),
product = str_replace(product, "(low|LOW|L\\sow|L),*", "low"),
product = str_replace(product, "(HIGH|Hi\\sgh|H)", "high"),
product = str_replace(product, "(FAT|Fat|F A T)", "fat"),
product = str_replace(product, "-liter", "_liter")) %>%
separate(product, into = c("V1", "V2", "V3", "V4"), sep = " ", extra = "merge")
# A tibble: 5 x 4
V1 V2 V3 V4
<chr> <chr> <chr> <chr>
1 milk 1_liter low fat
2 milk 1_liter low fat
3 milk 1_liter low fat
4 milk 1_liter low fat
5 milk 1.5_liter high fat
答案 1 :(得分:0)
另一个可能更广泛的选择:
out = data.frame(
V1=gsub("(.*) \\d.*","\\1",z$z),
V2=gsub(".*(\\d.*liter).*","\\1",z$z),
V3=gsub(".*liter (.*)[F|f].*","\\1",z$z),
V4=gsub(".*([f|F].*)","\\1",z$z),
stringsAsFactors=FALSE)
out[]=apply(out[],2,function(x) gsub(" ","",x))
> out
V1 V2 V3 V4
1 milk 1-liter low, fat
2 MILK 1-liter Low fat
3 Milk. 1_liter LOW FAT
4 Milk 1_liter L FAT
5 MILK 5_liter High FAT
apply()
仅用于删除空格。
数据
z= structure(list(z = c("milk 1-liter low, fat", "M I L K 1-liter L ow fat",
"Milk. 1_liter LOW FAT", "Milk 1_liter L F A T", "MILK 1.5_liter Hi gh FAT"
)), row.names = c(NA, -5L), class = "data.frame")