如何获取列中第二次出现的字符串并使用R重命名它?

时间:2018-02-10 18:41:25

标签: r

我有一个数据集$Plaza,其行名为 -

"Main Plaza 1"

"Main Plaza 2"

"Main Plaza 3"

"Main Plaza 1"

"Main Plaza 5"

我必须将'Main Plaza 1'的第二次出现重命名为'Main Plaza 1_second'

我尝试过以下代码 -

library("dplyr")

d <- grep("Main Plaza 1", dataset$Plaza)

for(i in length(d))

{

ifelse(length(d) == 2,
       str_replace(dataset$Plaza, grep("^Main Lane Plaza 1"),
                                      "Main Lane Plaza 1(second)"), NA)
break()

}

我已尝试多次使用更多代码无法找到解决方案。请帮忙!

2 个答案:

答案 0 :(得分:1)

为了将_second添加到出现多次的每个字符串,您可以使用:

plaza <- c("Main Plaza 1", "Main Plaza 2", "Main Plaza 3", "Main Plaza 1", "Main Plaza 5")
dups <- duplicated(plaza)
plaza[dups] <- paste0(plaza[dups], "_second")
plaza
## [1] "Main Plaza 1"        "Main Plaza 2"        "Main Plaza 3"        "Main Plaza 1_second" "Main Plaza 5"

为了仅修改“Main Plaza 1”的第二次出现(但保持其他任何重复的元素不变),请使用:

plaza <- c("Main Plaza 1", "Main Plaza 2", "Main Plaza 3", "Main Plaza 1", "Main Plaza 5")
i <- which(plaza == "Main Plaza 1")[2]
plaza[i] <- paste0(plaza[i], "_second")
plaza
## [1] "Main Plaza 1"        "Main Plaza 2"        "Main Plaza 3"        "Main Plaza 1_second" "Main Plaza 5"

答案 1 :(得分:1)

您可以简单地为具有所需值的行的数据帧进行子集化,然后将第二次出现的索引编入索引,如下所示:

pizza = data.frame(Name = c("Main Plaza 1",
    "Main Plaza 2",
    "Main Plaza 3",
    "Main Plaza 1",
    "Main Plaza 5"),
    stringsAsFactors = FALSE)

pizza[which(pizza$Name == "Main Plaza 1"),][2] <- "Main Lane Plaza 1(second)"

pizza

结果:

                       Name
1              Main Plaza 1
2              Main Plaza 2
3              Main Plaza 3
4 Main Lane Plaza 1(second)
5              Main Plaza 5

我肯定会尝试开始习惯在R中循环子集,几乎总是这样。