我已经编写了以下代码,根据上一列中的某些条件来创建新列。我相信我需要添加其他条件来保留以前的值。
BP2018_spread <- BP2018_spread %>%
dplyr :: mutate(Period_Type = if_else(grepl("QTD",BP2018_spread$Month), 'QTD', if_else(grepl("YTD",BP2018_spread$Month),'YTD', 'NA')))
Current Output:
Month Period_Type
<chr> <chr>
1 " Apr" NA
2 " Apr QTD" QTD
3 " Apr YTD" YTD
4 " Aug" NA
5 " Aug QTD" QTD
6 " Aug YTD" YTD
7 " Dec" NA
8 " Dec QTD" QTD
9 " Dec YTD" YTD
10 " Feb" NA
Desired Output:
Month Period_Type
<chr> <chr>
1 " Apr" Apr
2 " Apr QTD" QTD
3 " Apr YTD" YTD
4 " Aug" Aug
5 " Aug QTD" QTD
6 " Aug YTD" YTD
7 " Dec" Dec
8 " Dec QTD" QTD
9 " Dec YTD" YTD
10 " Feb" Feb
OR
Desired Output:
Month Period_Type
<chr> <chr>
1 " Apr" MTD
2 " Apr QTD" QTD
3 " Apr YTD" YTD
4 " Aug" MTD
5 " Aug QTD" QTD
6 " Aug YTD" YTD
7 " Dec" MTD
8 " Dec QTD" QTD
9 " Dec YTD" YTD
10 " Feb" MTD
答案 0 :(得分:2)
你能尝试
BP2018_spread <- BP2018_spread %>%
dplyr::mutate(Period_Type = ifelse(grepl("QTD", Month), 'QTD',
ifelse(grepl("YTD", Month),'YTD', 'MTD')))
答案 1 :(得分:2)
使用case_when
,可以避免编写多个封装的if_else
语句:
BP2018_spread <- data.frame(
Month = c(
" Apr",
" Apr QTD",
" Apr YTD",
" Aug",
" Aug QTD",
" Aug YTD",
" Dec",
" Dec QTD",
" Dec YTD",
" Feb"
))
BP2018_spread %>%
mutate(Period_Type=case_when(grepl("QTD",BP2018_spread$Month) ~ 'QTD',
grepl("YTD",BP2018_spread$Month) ~ 'YTD',
TRUE ~ as.character(Month)))
输出:
Month Period_Type
1 Apr Apr
2 Apr QTD QTD
3 Apr YTD YTD
4 Aug Aug
5 Aug QTD QTD
6 Aug YTD YTD
7 Dec Dec
8 Dec QTD QTD
9 Dec YTD YTD
10 Feb Feb
答案 2 :(得分:1)
我认为这可以帮助您
BP2018_spread %>%
mutate(Period_Type = if_else(grepl("QTD",Period_Type) | grepl("YTD",Period_Type),
as.character(Period_Type),
as.character(Month) ))
使用此数据:
#reproduce your dataset
BP2018_spread <- data.frame(
Month = c(
" Apr",
" Apr QTD",
" Apr YTD",
" Aug",
" Aug QTD",
" Aug YTD",
" Dec",
" Dec QTD",
" Dec YTD",
" Feb"
),
Period_Type = c(NA,
"QTD",
"YTD",
NA,
"QTD",
"YTD",
NA,
"QTD",
"YTD",
NA)
)
答案 3 :(得分:0)
您也可以尝试此方法
df <- read.csv("D:/X-tras/stackoverflow/STACK_YTD.csv",header = T)
v<- c("QTD","YTD")
df <- df %>%
mutate(final = ifelse(grepl("YTD|QTD",Period_Type), str_extract(Period_Type, str_c(v, collapse = "|")), "MTD"))
答案 4 :(得分:0)
BP2018_spread <- tibble(
Month = c(
" Apr",
" Apr QTD",
" Apr YTD",
" Aug",
" Aug QTD",
" Aug YTD",
" Dec",
" Dec QTD",
" Dec YTD",
" Feb"
)
)
BP2018_spread %>%
mutate(Period_Type = if_else(
grepl("QTD", Month),'QTD',
if_else(grepl("YTD", Month),'YTD',
trimws(Month)
)))
OR
BP2018_spread %>%
mutate(Period_Type = case_when(
grepl("QTD", Month) ~ 'QTD',
grepl("YTD", Month) ~ 'YTD',
TRUE ~ trimws(Month)
))
BP2018_spread %>%
mutate(Period_Type = if_else(
grepl("QTD", Month),'QTD',
if_else(grepl("YTD", Month),'YTD',
'MTD'
)))
OR
BP2018_spread %>%
mutate(Period_Type = case_when(
grepl("QTD", Month) ~ 'QTD',
grepl("YTD", Month) ~ 'YTD',
TRUE ~ 'MTD'
))
在两种情况下,我都使用trimws
来修剪空格,而且您也不需要BP2018_spread$
,因为mutate
可以理解列名。每个输出下的第二个代码选项使用case_when
,它更易于编写和理解。