[英]remove a character from the entire data frame
I have a dataframe with various columns, Some of the data within some columns contain double quotes, I want to remove these, for eg:我有一个包含各种列的 dataframe,某些列中的某些数据包含双引号,我想删除这些,例如:
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
I would use lapply
to loop over the columns and then replace the "
using gsub
.我会使用
lapply
循环遍历列,然后使用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
and if need the class
can be changed with type.convert
如果需要的
class
可以被改变type.convert
df1[] <- lapply(df1, type.convert)
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))
One option would be to use apply()
along with the gsub()
function to remove all double quotation marks:一种选择是使用
apply()
和gsub()
函数来删除所有双引号:
df <- data.frame(ID=c("\"1", "\"2"),
name=c("x", "y"),
value1=c("a,\"b,\"c", "d,\"r\""),
value2=c("x\"", "z\""))
df <- data.frame(apply(df, 2, function(x) {
x <- gsub("\"", "", x)
})
> df
ID name value1 value2
1 1 x a,b,c x
2 2 y d,r z
To remove $
you have to escape it \\\\\\$
.要删除
$
你必须转义它\\\\\\$
。 Try:尝试:
df[] <- lapply(df, gsub, pattern="\\\$", replacement="")
A dplyr solution (based on the suggestion of @akrun in one of the comments). dplyr 解决方案(基于@akrun 在其中一条评论中的建议)。
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))
df1 <- df1 %>% dplyr::mutate(across(everything(), stringr::str_remove_all, pattern = '"'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.