【问题标题】:How to copy data from a column to another based on a condition in R?如何根据 R 中的条件将数据从列复制到另一列?
【发布时间】:2018-08-14 09:21:05
【问题描述】:

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

  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。

【问题讨论】:

  • 试试i1 &lt;- with(df1, grepl("^(EMEA|AME)", Funct.Area); df1$Funct.Area[i1] &lt;- df1$Func_Environment[i1]
  • 知道了!太感谢了!我将使用适合我的正确解决方案编辑帖子。

标签: r data-science data-cleaning


【解决方案1】:

我们使用grepl 创建一个逻辑索引 ('i1'),以检查字符串开头 (^) 的字符是“EMEA”还是(|)“AME”。使用它,将 'Funct.Area' 的元素替换为 'Funct_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"))

【讨论】:

  • 嗨!感谢您的评论。它使我陷入错误。 i1 &lt;- with(tickets$Funct.Area, grepl("^(EMEA|AME)")) Error in eval(substitute(expr), data, enclos = parent.frame()) : numeric 'envir' arg not of length one你知道第一句话可能错了吗?
  • @nsonia 如果你检查我的代码,我没有做with(df1$,它是with(df1, grepl
  • 谢谢,我已经更正了代码。现在我提出了因素问题,这是我在第二句话中收到的输出。 tickets$Funct.Area[i1] &lt;- tickets$Func_Environment[i1] Warning message: In [(*tmp*, i1, value = c(22L, NA, NA, 8L, NA, 34L, : invalid factor level, NA generated
  • 对不起,我没有完成评论就点击了进入。您可以在上面找到问题。
  • @nsoria 好的,您有一个 factor 列。在做作业之前最好有一个character 类列,即df1$Funct.Area &lt;- as.character(df1$Funct.Area)。我已经更新了我使用的数据。
猜你喜欢
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多