ID,描述
1,DRIVING VEHICLE ON HIGHWAY WITHOUT CURRENT REGISTRATION PLATES AND VALIDATION TABS
2,EXCEEDING SPEED LIMIT IN SCHOOL ZONE WITH 35 MPH IN 25 ZONE
3,DRIVING VEHICLE IN EXCESS OF REASONABLE AND PRUDENT SPEED ON HIGHWAY 8855
如果我想用" EXCESS SPEED"替换整个描述行。如果它包含" SPEED"。 我正在使用gsub函数:
training_data$Description = gsub("(dot)(star)SPEED(dot)(star)","EXCESS SPEED",training_data$Description,fixed = TRUE)
它仍然不起作用我得到了结果(只需更换" SPEED"使用" EXCESS SPEED"而不是整行):
收到的输出:
id,Description
1,DRIVING VEHICLE ON HIGHWAY WITHOUT CURRENT REGISTRATION PLATES AND VALIDATION TABS
2,EXCEEDING EXCESS SPEED LIMIT IN SCHOOL ZONE WITH 35 MPH IN 25 ZONE
3,DRIVING VEHICLE IN EXCESS OF REASONABLE AND PRUDENT EXCESS SPEED ON HIGHWAY 8855
预期产出:
id,Description
1,DRIVING VEHICLE ON HIGHWAY WITHOUT CURRENT REGISTRATION PLATES AND VALIDATION TABS
2,EXCESS SPEED
3,EXCESS SPEED
答案 0 :(得分:0)
你可以试试这个:
training_data$Description = sapply(training_data[,2],function(sentence){
if(grepl("SPEED",sentence)){
as.character("EXCESS SPEED")
}else{
as.character(sentence)
}
})