替换dataframe中变量字符串的部分。

时间:2022-01-17 16:49:22

I have data frame:

我有数据帧:

     SINGLE_MEM                     crd    
1          5        -97.75514500005232 30.263874000727306
2          8        -97.81095799930782 30.234157000528484))) 
3          3        ((-97.68044500036247 30.267636999839592 
4          2        -97.71808637589912 30.1848525102668))  
5         10        -97.76847799967814 30.432537999903005  
6          6        -97.6937453403672  30.46382579769979  

In crd column i have coordinates in character. I need to replace all '( )' on a gaps. I replace ')', '))', ')))' by

在crd列中,我有字符的坐标。我需要替换空白上的所有'()'。我替换')',')')','),')by

df$crd <- gsub(")))", "", df$crd)
df$crd <- gsub("))", "", df$crd)
df$crd <- gsub(")", "", df$crd)

but i cant replace a '(', '((', '(((' because i have an error: Error in gsub ("((", "", df $ crd): Incorrect regular expression '((', reason 'Missing') ''

但是我不能替换一个'(',',','()(因为我有一个错误:gsub中的错误(",",",df $ crd):不正确的正则表达式'(',原因'Missing')”

1 个解决方案

#1


2  

You can use

您可以使用

gsub("[()]+", "", df$crd)

#1


2  

You can use

您可以使用

gsub("[()]+", "", df$crd)