我試圖刪除包含多個字符串的數據框中最後兩個單詞之間的空白。我曾嘗試使用gsub
,但我的嘗試gsub("(\\s){1}$","",df1$V1)
似乎相當錯誤! df1
是我的數據集,df2
是我之後的結果。刪除R中的字符串中最後兩個單詞之間的空白
df1 <- data.frame(V1=c("Apple Pear Orange, AAA 111", "Grapes Banana Pear . BBB 222", "Orange Kiwi Melon , CCC 333", "Apple DDD 444", "Kiwi Melon Orange CCC 333", "Apple Pear Orange, AAA 111", "Tomato Cucumber EEE 222", "Seagull Pigeon ZZZ 111"), stringsAsFactors = F)
df2 <- data.frame(V1=c("Apple Pear Orange, AAA111", "Grapes Banana Pear . BBB222", "Orange Kiwi Melon , CCC333", "Apple DDD444", "Kiwi Melon Orange CCC333", "Apple Pear Orange, AAA111", "Tomato Cucumber EEE222", "Seagull Pigeon ZZZ111"), stringsAsFactors = F)
這適用於我真實數據集中的所有字符串。謝謝。 – Chris