我需要从数据框中删除所有撇号,但是一旦我使用....
textDataL <- gsub("'","",textDataL)
当我只想从可能存在的任何文本中删除任何撇号时,数据框会被破坏并且新数据框只包含值和NA?我是否遗漏了撇号和数据框明显的东西?
答案 0 :(得分:2)
保持结构完整:
dat1 <- data.frame(Col1= c("a woman's hat", "the boss's wife", "Mrs. Chang's house", "Mr Cool"),
Col2= c("the class's hours", "Mr. Jones' golf clubs", "the canvas's size", "Texas' weather"),
stringsAsFactors=F)
我会用
dat1[] <- lapply(dat1, gsub, pattern="'", replacement="")
或
library(stringr)
dat1[] <- lapply(dat1, str_replace_all, "'","")
dat1
# Col1 Col2
# 1 a womans hat the classs hours
# 2 the bosss wife Mr. Jones golf clubs
# 3 Mrs. Changs house the canvass size
# 4 Mr Cool Texas weather
答案 1 :(得分:1)
您不希望直接在数据框上应用gsub
,而是以列为单位,例如
apply(textDataL, 2, gsub, pattern = "'", replacement = "")