这是我的表格
产品 | 销售天数 | |
---|---|---|
谷歌 | MTWTHFSASU | 28000 |
苹果 | THFSASU | 10000 |
由于要提取的字符重叠,因此最好使用str_extract_all
将每个字符与OR(|
)提取到list
列中,获取list
的lengths
以创建"No_of_Days",将"销售额"除以"No_of_Days"并unnest
列表"列
library(dplyr)
library(stringr)
library(tidyr)
df1 %>%
mutate(Days = str_extract_all(Days, "M|T|W|TH|F|SA|SU"),
No_of_Days = lengths(Days),
Sales = Sales/No_of_Days) %>%
unnest(c(Days))
-输出
# A tibble: 11 x 4
# Product Days Sales No_of_Days
# <chr> <chr> <dbl> <int>
# 1 Google M 4000 7
# 2 Google T 4000 7
# 3 Google W 4000 7
# 4 Google T 4000 7
# 5 Google F 4000 7
# 6 Google SA 4000 7
# 7 Google SU 4000 7
# 8 Apple T 2500 4
# 9 Apple F 2500 4
#10 Apple SA 2500 4
#11 Apple SU 2500 4
数据
df1 <- structure(list(Product = c("Google", "Apple"), Days = c("MTWTHFSASU",
"THFSASU"), Sales = c(28000L, 10000L)), class = "data.frame",
row.names = c(NA,
-2L))