r
  • string
  • 2017-01-29 96 views 2 likes 
    2

    我有这样一个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查询。

    回答

    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'" 
    

    如果你想elimenate围绕上述结果的双引号,以及,你再次包裹在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' 
    
    +1

    我的预期输出我想了一个小错误。 。时间之间不应该有逗号,但我通过在答案中调整了崩溃参数来修复它。谢谢 ! – vagabond

    相关问题