我需要在具有多个条件的数据框中改变列的值。
df <- data.frame(id = c(1,2,3,4,5,6,7,8,9,10),
city = c("Newyork", "NewYork", "NewYrk", "newYork", "Newyork",
"London","Londonn","Mumbai","mumbai","Bombay")
)
我需要mutate
基于以下条件的城市列值
new
(不区分大小写)开头,然后Newyork
Lond
开头,然后是London
mum
或bom
开头(不区分大小写)时,Mumbai
基于base-r和dplyr
的解决方案都很不错。
我尝试了什么?
df[df$city %in% c('NewYork', 'NewYrk', 'newYork' ),]$city <- "Newyork"
这会完成#1,但不完全符合条件 - 它以new
开头(不区分大小写)
答案 0 :(得分:0)
您可以使用嵌套的routeTemplate: "api/{controller}/{action}/{correlationId}/{symbol}"
语句和ifelse
来完成此操作,即
substring
答案 1 :(得分:0)
这个怎么样?不确定如何为最后一个条件执行正则表达式的OR:
# look for 'new' then use the index to subset city and replace with Newyork
df$city[grep(pattern = "new", x = df$city, ignore.case = TRUE)] <- "Newyork"
# look for 'lond' then use the index to subset city and replace with London
df$city[grep(pattern = "Lond", x = df$city, ignore.case = TRUE)] <- "London"
# look for 'mum' then use the index to subset city and replace with Mumbai
df$city[grep(pattern = "mum", x = df$city, ignore.case = TRUE)] <- "Mumbai"
# look for 'bom' then use the index to subset city and replace with Mumbai
df$city[grep(pattern = "bom", x = df$city, ignore.case = TRUE)] <- "Mumbai"
# OR
df$city[grep(pattern = "bom|mum", x = df$city, ignore.case = TRUE)] <- "Mumbai"