代码之家  ›  专栏  ›  技术社区  ›  nariver1

如何根据R中的条件将数据从一列复制到另一列?

  •  1
  • nariver1  · 技术社区  · 7 年前

    我有如下数据框,如下所示。

      Funct.Area Environment ServiceType Ticket.Nature SLA.Result..4P. IRIS.Priority Func_Environment
    2        FUN         DCF         FUN            SR              OK        Medium          FUN-DCF
    3  AME - FIN         DCF         FUN            SR          Defect        Medium    AME - FIN-DCF
    4  EMEA -FIN         DCF         FUN            SR              OK        Medium    EMEA -FIN-DCF
    5        APS         DCF         APS            SR          Defect        Medium          APS-DCF
    6   EMEA -SC         DCF         FUN            SR              OK        Medium     EMEA -SC-DCF
    7        SEC         DCF         SEC            SR              OK           Low          SEC-DCF
    

    我需要从字段中做一个子集 Funct.Area 为了获得前3或4个字符,如果是“EMEA”或“AME”,则将该字段中的值替换为 Environment

    我在StackOverflow中也研究过类似的问题,但我无法管理复制部分,因为我遇到了一个因素问题。

    如果有什么方法我应该尝试,是否有人能指导我?

    谢谢

    编辑#1: 按照akrun进近。

    tickets$Funct.Area <- as.character(tickets$Funct.Area)
    i1 <- with(tickets, grepl("^(EMEA|AME)", tickets$Funct.Area))
    tickets$Funct.Area[i1] <- tickets$Func_Environment[i1]
    head(tickets)
      Funct.Area Environment ServiceType Ticket.Nature SLA.Result..4P. IRIS.Priority Func_Environment
    2        FUN         DCF         FUN            SR              OK        Medium          FUN-DCF
    3         13         DCF         FUN            SR          Defect        Medium    AME - FIN-DCF
    4         65         DCF         FUN            SR              OK        Medium    EMEA -FIN-DCF
    5        APS         DCF         APS            SR          Defect        Medium          APS-DCF
    6         66         DCF         FUN            SR              OK        Medium     EMEA -SC-DCF
    7        SEC         DCF         SEC            SR              OK           Low          SEC-DCF
    

    编辑2:

    这是对我有效的解决方案。

    tickets$Funct.Area <- as.character(tickets$Funct.Area)
    tickets$Environment <- as.character(tickets$Environment)
    i1 <- with(tickets, grepl("^(EMEA|AME)", tickets$Funct.Area))
    tickets$Funct.Area[i1] <- tickets$Func_Environment[i1]
    tickets$Funct.Area[i1] <- as.character(tickets$Environment[i1])
    

    非常感谢您@akrun。

    1 回复  |  直到 7 年前
        1
  •  0
  •   akrun    7 年前

    我们使用 grepl 检查开头的字符( ^ )字符串的“EMEA”或( | )“AME”。使用该函数替换“Funct”的元素。带有“Funct\u Environment”的区域

    i1 <- with(df1, grepl("^(EMEA|AME)", Funct.Area))
    df1$Funct.Area[i1] <- df1$Func_Environment[i1] 
    df1
    #     Funct.Area Environment ServiceType Ticket.Nature SLA.Result..4P. IRIS.Priority Func_Environment
    #2           FUN         DCF         FUN            SR              OK        Medium          FUN-DCF
    #3 AME - FIN-DCF         DCF         FUN            SR          Defect        Medium    AME - FIN-DCF
    #4 EMEA -FIN-DCF         DCF         FUN            SR              OK        Medium    EMEA -FIN-DCF
    #5           APS         DCF         APS            SR          Defect        Medium         APS-DCF'
    #6  EMEA -SC-DCF         DCF         FUN            SR              OK        Medium     EMEA -SC-DCF
    #7           SEC         DCF         SEC            SR              OK           Low          SEC-DCF
    

    数据

    df1 <- structure(list(Funct.Area = c("FUN", "AME - FIN", "EMEA -FIN", 
    "APS", "EMEA -SC", "SEC"), Environment = c("DCF", "DCF", "DCF", 
    "DCF", "DCF", "DCF"), ServiceType = c("FUN", "FUN", "FUN", "APS", 
    "FUN", "SEC"), Ticket.Nature = c("SR", "SR", "SR", "SR", "SR", 
    "SR"), SLA.Result..4P. = c("OK", "Defect", "OK", "Defect", "OK", 
    "OK"), IRIS.Priority = c("Medium", "Medium", "Medium", "Medium", 
    "Medium", "Low"), Func_Environment = c("FUN-DCF", "AME - FIN-DCF", 
     "EMEA -FIN-DCF", "APS-DCF'", "EMEA -SC-DCF", "SEC-DCF")), .Names = c("Funct.Area", 
     "Environment", "ServiceType", "Ticket.Nature", "SLA.Result..4P.", 
    "IRIS.Priority", "Func_Environment"), class = "data.frame", 
     row.names = c("2", 
     "3", "4", "5", "6", "7"))