将dataframe列转换为不带双引号的连接字符串

时间:2017-01-29 15:19:58

标签: r string

我有一个像这样的data.frame:

df = data.frame(str_col = c("when xyz = 'some_string' then 'this_string'",
                            "when xyz = 'some_string2' then 'this_string'"))

我想将str_col转换为逗号分隔的向量,而不使用双引号,如:

c(when xyz = 'some_string' then 'this_string', when xyz = 'some_string2' then 'this_string')

我在尝试这个: str_vec <- paste(shQuote(df$str_col), collapse=",")

但我明白了:

[1] "\"when xyz = 'some_string' then 'this_string'\",\"when xyz = 'some_string2' then 'this_string'\""

我不想反斜杠或双引号。我正在尝试构建一个SQL查询。

1 个答案:

答案 0 :(得分:2)

我认为您应该使用noquote代替shQuote

str_vec <- paste(noquote(df$str_col), collapse=",")

给出:

> str_vec
[1] "when xyz = 'some_string' then 'this_string',when xyz = 'some_string2' then 'this_string'"

如果你想在上面的结果周围使用双引号,你可以在noquote再次包装它:

str_vec <- noquote(paste(noquote(df$str_col), collapse=","))

给出:

> str_vec
[1] when xyz = 'some_string' then 'this_string',when xyz = 'some_string2' then 'this_string'
相关问题