假設我有以下資料:
df = data.frame(x = c("ciao he is a good researcher","he is working well on political franchise",
"gionny and I have to sort this issue","r leave us in peace"),stringsAsFactors = F)
rep = data.frame(x=c("political_risk","good_researcher"),stringsAsFactors = F)
我想將“rep”替換為“df”。
我曾經運行此代碼,但現在出現錯誤 ( Error in UseMethod("type") : no applicable method for 'type' applied to an object of class "data.frame"):
result = df %>% str_replace_all(rep)
如果我這樣做,它會起作用:
str_replace_all(df$x, c("political risk" = "political_risk", "good researcher" = "good_researcher"))
然而,我想一口氣做到。
誰能幫我?
謝謝!
uj5u.com熱心網友回復:
我們可能需要一個命名向量
library(dplyr)
library(stringr)
df %>%
mutate(x = str_replace_all(x,
setNames(rep$x, c("political risk", "good researcher"))))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338642.html
上一篇:如何洗掉模態對話框上的關閉按鈕?
下一篇:如何從這些資料創建一個新列?
