如何在数据帧中链接多个字符串值更改?

时间:2017-08-15 17:34:11

标签: r string dataframe replace

如果以下是我的数据框

修改变量名称

forces2015 <- data.frame(Country = c("Russian Federation", "Egypt, Arab Rep.", "Iran, Islamic Rep."))

现在我想在一个函数中更改所有单个值,但我只能想到这个:

修改列名:

forces2015$Country <- gsub("Russian Federation", "Russia", forces2015$Country) 
forces2015$Country <- gsub("Egypt, Arab Rep.", "Egypt", forces2015$Country)  
forces2015$Country <- gsub("Iran, Islamic Rep.", "Iran", forces2015$Country)

我可能有10个这样的值,所以如何以最优雅的方式解决这个问题呢?

4 个答案:

答案 0 :(得分:3)

您可以使用recode包中的dplyr,如下所示:

forces2015$Country <- recode(forces2015$Country, "Russian Federation" = "Russia", "Egypt, Arab Rep." = "Egypt", "Iran, Islamic Rep." = "Iran")

答案 1 :(得分:1)

您可以试用qdap套餐。

c1 <- c('Russian Federation', 'Egypt, Arab Rep.', 'Iran, Islamic Rep.')
c2 <- c('Russia', 'Egypt', 'Iran')

library(qdap)    
mgsub(c1,c2, forces2015$Country.Name)

答案 2 :(得分:1)

使用dplyr :: case_when ...

library(dplyr)

forces2015 %>%
  mutate(Country = case_when(
    Country == "Russian Federation" ~ "Russia",
    Country == "Egypt, Arab Rep." ~ "Egypt",
    Country == "Iran, Islamic Rep." ~ "Iran",
    TRUE ~ Country))

答案 3 :(得分:0)

这样可以将数据框过滤到相关行,并快速替换Country值。这对单个替换有好处: data$Country[data$Country=="Russian Federation"] <- "Russia"

如果要使用许多条件替换多个值,只使用基本库函数,我建议使用一系列ifelse()函数:

forces2015$Country <- with(forces2015,
                   ifelse(Country=="Russian Federation","Russia",
                      ifelse(Country=="Egypt, Arab Rep.","Egypt",
                         ifelse(Country=="Iran, Islamic Rep.","Iran",
                            Country))))