[英]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
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.