我試圖刪除數據框的一個字符串元素中存在的'+'字符。但我無法找到解決辦法。如何用R中的gsub()函數替換'+'
以下是數據框。
txtdf <- structure(list(ID = 1:9, Var1 = structure(c(1L, 1L, 1L, 1L, 4L,
5L, 5L, 2L, 3L), .Label = c("government", "parliament", "parliment",
"poli+tician", "politician"), class = "factor")), .Names = c("ID",
"Var1"), class = "data.frame", row.names = c(NA, -9L))
# ID Var1
# 1 government
# 2 government
# 3 government
# 4 government
# 5 poli+tician
# 6 politician
# 7 politician
# 8 parliament
# 9 parliment
我嘗試了兩種方式,他們都沒有給出預期的結果:
WAY1
txtdf <- gsub("[:punct:]","", txtdf)
# [1] "goverme" "goverme" "goverme" "goverme" "oli+iia" "oliiia" "oliiia"
# [8] "arliame" "arlime"
我不明白什麼是錯在這裏。我希望單獨第5個元素的'+'字符被替換爲沒有值,但是所有元素都如上編輯。
Way2
txtdf<-gsub("*//+","",txtdf)
# [1] "government" "government" "government" "government" "poli+tician"
# [6] "politician" "politician" "parliament" "parliment"
在這裏有根本沒有變化。我想我嘗試過的是,我試圖用雙斜槓來逃避+角色。
逸出使用'做'\\。 – Rahul
或者把它放在一個字符類中:'「[+]」'。由於'+'(1或更多)在字符類中沒有特殊含義,因此不需要轉義。 – LukStorms
或使用'fixed'參數:'gsub(「+」,「」,txtdf $ varname,fixed = TRUE)' – user2957945