数据帧R中的Gsub撇号 [英] Gsub apostrophe in data frame R

查看:171
本文介绍了数据帧R中的Gsub撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据框中删除所有撇号,但一旦我使用....

I need to remove all apostrophes from my data frame but as soon as I use....

textDataL <- gsub("'","",textDataL)

数据框被破坏,新数据框架只包含值和NAs,当我只想从任何可能在那里的文本中删除任何撇号?我使用撇号和数据框缺少一些明显的东西?

The data frame gets ruined and the new data frame only contains values and NAs, when I am only looking to remove any apostrophes from any text that might be in there? Am I missing something obvious with apostrophes and data frames?

推荐答案

保持结构不变:

 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

这篇关于数据帧R中的Gsub撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆