R语言生成专属Coupon.URL遇替换长度错误及ifelse覆盖问题求助
解决方法
首先纠正你代码里的笔误:之前赋值coupon_value时,有一行错误地写了Balance.of.Points.null,应该统一为Balance.of.Points,否则会因列不存在报错或出现逻辑错误。
方法1:嵌套ifelse保留原有值
如果坚持用ifelse实现,每次else分支不要设为NA,而是保留当前已有的Coupon.URL值,这样就不会覆盖之前生成的结果:
- 先初始化
Coupon.URL为缺失值:
clutch_coupons$Coupon.URL <- NA_character_
- 按积分区间依次赋值,每次保留原有结果:
# 区间1:积分 ≤ 499 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points <= 499, paste0("https://offer.com/1dollar?", clutch_coupons$Head.of.household.id), clutch_coupons$Coupon.URL ) # 区间2:500 ≤ 积分 < 1000 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points >= 500 & clutch_coupons$Balance.of.Points < 1000, paste0("https://offer.com/850dollar?", clutch_coupons$Head.of.household.id), clutch_coupons$Coupon.URL ) # 区间3:1000 ≤ 积分 < 1500 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points >= 1000 & clutch_coupons$Balance.of.Points < 1500, paste0("https://offer.com/850dollar?", clutch_coupons$Head.of.household.id), # 替换为该区间对应的URL前缀 clutch_coupons$Coupon.URL ) # 区间4:1500 ≤ 积分 < 2000 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points >= 1500 & clutch_coupons$Balance.of.Points < 2000, paste0("https://offer.com/12dollar?", clutch_coupons$Head.of.household.id), clutch_coupons$Coupon.URL ) # 区间5:2000 ≤ 积分 < 2500 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points >= 2000 & clutch_coupons$Balance.of.Points < 2500, paste0("https://offer.com/15dollar?", clutch_coupons$Head.of.household.id), clutch_coupons$Coupon.URL ) # 区间6:2500 ≤ 积分 < 3100 clutch_coupons$Coupon.URL <- ifelse( clutch_coupons$Balance.of.Points >= 2500 & clutch_coupons$Balance.of.Points < 3100, paste0("https://offer.com/20dollar?", clutch_coupons$Head.of.household.id), clutch_coupons$Coupon.URL )
方法2:用dplyr的case_when(更简洁易读)
如果安装了dplyr包,用case_when处理多分支条件会更清晰,代码可读性更高:
library(dplyr) clutch_coupons <- clutch_coupons %>% mutate(Coupon.URL = case_when( Balance.of.Points <= 499 ~ paste0("https://offer.com/1dollar?", Head.of.household.id), Balance.of.Points >= 500 & Balance.of.Points < 1000 ~ paste0("https://offer.com/850dollar?", Head.of.household.id), Balance.of.Points >= 1000 & Balance.of.Points < 1500 ~ paste0("https://offer.com/850dollar?", Head.of.household.id), # 替换为对应前缀 Balance.of.Points >= 1500 & Balance.of.Points < 2000 ~ paste0("https://offer.com/12dollar?", Head.of.household.id), Balance.of.Points >= 2000 & Balance.of.Points < 2500 ~ paste0("https://offer.com/15dollar?", Head.of.household.id), Balance.of.Points >= 2500 & Balance.of.Points < 3100 ~ paste0("https://offer.com/20dollar?", Head.of.household.id), TRUE ~ NA_character_ # 处理所有未匹配的情况 ))
你之前警告的原因
你之前的代码出现number of items to replace is not a multiple of replacement length警告,是因为右边的paste0("https://offer.com/850dollar?", clutch_coupons$Head.of.household.id)生成了整个列的长度,而左边是符合条件的子集行,两者长度不匹配。正确的子集赋值写法应为:
condition <- clutch_coupons$Balance.of.Points >= 500 & clutch_coupons$Balance.of.Points < 1000 clutch_coupons$Coupon.URL[condition] <- paste0("https://offer.com/850dollar?", clutch_coupons$Head.of.household.id[condition])
内容的提问来源于stack exchange,提问作者lomingchun
相关产品推荐
相关产品推荐

