从整个数据框中删除一个字符 [英] remove a character from the entire data frame

查看:57
本文介绍了从整个数据框中删除一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不同列的数据框,某些列中的某些数据包含双引号,我想删除它们,例如:

I have a dataframe with various columns, Some of the data within some columns contain double quotes, I want to remove these, for eg:

ID    name   value1     value2
"1     x     a,"b,"c     x"
"2     y     d,"r"       z"

我希望它看起来像这样:

I want this to look like this:

ID    name   value1    value2
1     x      a,b,c      x
2     y      d,r        z


推荐答案

我将使用 lapply 循环遍历各列,然后将 替换为 gsub

I would use lapply to loop over the columns and then replace the " using gsub.

df1[] <- lapply(df1, gsub, pattern='"', replacement='')
df1
#  ID name value1 value2
#1  1    x  a,b,c      x
#2  2    y    d,r      z

如果需要,可以使用<$更改 c $ c> type.convert

and if need the class can be changed with type.convert

df1[] <- lapply(df1, type.convert)



数据



data

df1 <-  structure(list(ID = c("\"1", "\"2"), name = c("x", "y"),
value1 = c("a,\"b,\"c", 
"d,\"r\""), value2 = c("x\"", "z\"")), .Names = c("ID", "name", 
"value1", "value2"), class = "data.frame", row.names = c(NA, -2L))

这篇关于从整个数据框中删除一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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