簡體   English   中英

更改 r 中的觀察名稱

[英]Changing observation names in r

我有一個“客戶”數據集,其中一個變量是“類型”,它由 4 個觀察值組成——汽車保險、汽車保險、人壽保險和家庭財產/建築保險。 我想將“汽車保險”更改為“汽車保險”以進行 3 次觀察。 我嘗試了以下方法,但似乎都不起作用:

mutate(Type = ifelse(Type == "CAR INSURANCE", "car insurance", Type))
rename(Type, "CAR INSURANCE" == "car insurance")
Insurance_df = data.frame("Type" = c("CAR INSURANCE", "car insurance","life insurance",
                                     "home content/building insurance"))
Insurance_df = rename(Insurance_df, "car insurance" = "CAR INSURANCE")

有人可以指出我正確的方向嗎?

如果它是完全相同的字符串,您可以嘗試這樣的操作,以便僅獲取小寫字符串:

df <- data.frame("type" = c("CAR INSURANCE", "car insurance", "life insurance"))
df$type <- tolower(df$type)

或者,為了更靈活:

df[df$type == "CAR INSURANCE", ] <- "car insurance"

如果列 df$type 是一個因素(如果您使用的是 R < 4,這將是默認值),那么以下將更有效:

levels(df$type) <- tolower(levels(df$type))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM