r
  • string
  • 2017-01-29 95 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

    相關問題