简体   繁体   English

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

[英]How to copy data from a column to another based on a condition in R?

I have below data frame as shown below. 我有下面的数据框,如下所示。

  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

I need to do a subset from the field Funct.Area in order to have first 3 or 4 chars and if they are "EMEA" or "AME", then replace the value from that field with the one on Environment . 我需要从Funct.Area字段中Funct.Area一个子集,以具有第一个3或4个字符,如果它们是“ EMEA”或“ AME”,则将该字段中的值替换为Environment上的一个。

I have looked into similar question in StackOverflow but I am not able to manage the copy part since I'm getting stuck with a factor issue. 我已经在StackOverflow中调查了类似的问题,但是由于遇到了问题,因此无法管理副本部分。

Is someone able to guide me if there is any approach that should I try? 如果我应该尝试任何方法,有人可以指导我吗?

Thanks. 谢谢。

Edit #1: Per @akrun approach. 编辑#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

Edit 2: 编辑2:

This is the solution that worked for me. 这是对我有用的解决方案。

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])

Thank you very much @akrun. 非常感谢@akrun。

We create a logical index ('i1') using grepl to check if the characters at the start ( ^ ) of the string is either 'EMEA' or ( | ) "AME". 我们使用grepl创建一个逻辑索引('i1'),以检查字符串开头( ^ )处的字符是'EMEA'还是( | )“ AME”。 Using that, replace the elements of 'Funct.Area' with 'Funct_Environment' 使用它,将“ 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

data 数据

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"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据R中另一列的条件过滤列 - How to filter a column based on a condition from another column in R 根据条件从R中的另一个数据表中提取列值 - Extracting column values based on condition from another data table in R R:根据条件(不同大小的数据框)从另一个数据框的列中为列分配值 - R: Assign values to column, from a column from another data frame, based on a condition (different sized data frames) 在 R 中,如何根据条件将列中的值替换为另一个数据集的另一列的值? - In R, how to replace values in a column with values of another column of another data set based on a condition? R function 在第三种条件下将数据从一列复制到另一列 - R function to copy data from one column to another under a third condition (R) 如何根据 R 中的另一列和 ID 从一列复制粘贴值 - (R) How to copy paste values from one column based on another column and ID in R 如何在R中第一次出现另一个条件后根据条件的第二次出现从列中获取值? - How to get a value from a column based on second occurrence of a condition after first occurrence of another condition in R? 从基于 R 中另一列中的条件的列中获取值? - Obtain value from a column based off condition in another column in R? R:根据另一列的文本条件更新列 - R: Update Column Based on Text Condition from Another Column 根据另一列的条件在 R 中添加新列 - add new column in R based on condition from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM