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
.
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.
Is someone able to guide me if there is any approach that should I try?
Thanks.
Edit #1: Per @akrun approach.
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:
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.
We create a logical index ('i1') using grepl
to check if the characters at the start ( ^
) of the string is either 'EMEA' or ( |
) "AME". Using that, replace the elements of 'Funct.Area' with '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"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.