基于多个条件改变列值

时间:2017-04-08 06:38:49

标签: r

我需要在具有多个条件的数据框中改变列的值。

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基于以下条件的城市列值

  1. 当城市以new(不区分大小写)开头,然后Newyork
  2. 当城市以Lond开头,然后是London
  3. 当城市以mumbom开头(不区分大小写)时,Mumbai
  4. 基于base-r和dplyr的解决方案都很不错。

    我尝试了什么?

    df[df$city %in% c('NewYork', 'NewYrk', 'newYork' ),]$city <- "Newyork"
    

    这会完成#1,但不完全符合条件 - 它以new开头(不区分大小写)

2 个答案:

答案 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"