数据帧R中的Gsub撇号
我需要从我的数据框中删除所有撇号,但一旦我使用数据帧R中的Gsub撇号,r,dataframe,gsub,apostrophe,R,Dataframe,Gsub,Apostrophe,我需要从我的数据框中删除所有撇号,但一旦我使用 textDataL <- gsub("'","",textDataL) textDataL您不希望直接在数据帧上应用gsub,而是按列应用,例如 apply(textDataL, 2, gsub, pattern = "'", replacement = "") 要保持结构完整,请执行以下操作: dat1 <- data.frame(Col1= c("a woman's hat", "the boss's wife", "Mrs.
textDataL <- gsub("'","",textDataL)
textDataL您不希望直接在数据帧上应用gsub
,而是按列应用,例如
apply(textDataL, 2, gsub, pattern = "'", replacement = "")
要保持结构完整,请执行以下操作:
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我假设结果是矩阵。如果是这种情况,gsub(“'”,“”)as.matrix(textDataL)应该可以工作。
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