R-lang:如果等于引号,则删除第一个字符

R-lang: remove first character if equal to quotation mark

R新手。我正在尝试从数据框中一行的开头和结尾删除,"。如果引号不是第一个或最后一个字符,我不想删除。我不确定为什么以下内容对我不起作用数据的数据框,其中每一行都是文本的数据点。

引文不是字符串,而是正文的一部分

一行数据框看起来像这样:

x<-  '"hello world. She said, "hello again" it was a pleasant response"'

结果应该是:

x2 <- 'hello world. She said, "hello again" it was a pleasant response"'

我认为这行得通:

gsub("\n\"", "", df)

但是,这不起作用。建议?

您可以 trim 从字符串末尾开始使用引号,如下所示:

x <- gsub('"$','',x)

并且从字符串的开头开始这样:

x <- gsub('^"','',x)

因为字符 $^ 匹配字符串的结尾和开头。例如:

myData<-data.frame(foo=c('"asdf"','ASDF'),
                   bar=c('jkl;','"JKL;"'))
myData
#>     foo    bar
#>1 "asdf"   jkl;
#>2   ASDF "JKL;"

# trim the quote characters from myData$foo
myData$foo <- gsub("^\"|\"$", "", myData$foo)
myData

#>   foo    bar
#>1 asdf   jkl;
#>2 ASDF "JKL;"

另一个选项是 trimws,它将删除字符串开头或结尾的 all "

trimws(x, whitespace = '"')
#[1] "hello world. She said, \"hello again\" it was a pleasant response"

或者只从头开始:

trimws(x, "left", '"')
#[1] "hello world. She said, \"hello again\" it was a pleasant response\""

另一种选择可能是使用 startsWithsubstring

if(startsWith(x, '"')) substring(x, 2) else x
#[1] "hello world. She said, \"hello again\" it was a pleasant response\""