簡體   English   中英

如何重命名列中的值,因為我拼錯了它們並且無法在 R 或 Colab 中重命名它們

[英]How do I rename the values in my column as I have misspelt them and cant rename them in R or Colab

我有一個給我的數據框。 在標題為 state 的列下,有兩個名稱相同但區分大小寫的組件,即一個是“London”,另一個是“LONDON”。 我如何能夠將“倫敦”重命名為“倫敦”,以便將它們加在一起而不是分開。 提醒一下,我正在嘗試更改輸入的名稱而不是列的名稱。

也許您可以嘗試使用 case_when function。 我會做這樣的事情:

''''

變異(數據,State_def=case_when(State=="LONDON" ~ "London", State=="London" ~ "London", TRUE ~ NA_real_)

我可能會誤解,但我認為它應該像這樣簡單:

x$state <- sub( "LONDON", "London", x$state, fixed=TRUE ) 

這應該將倫敦更改為倫敦

您可以使用以下代碼,df 是您當前的 dataframe,其中您想將“LONDON”替換為“London”

df <- data.frame(Country = c("US", "UK", "Germany", "Brazil","US", "Brazil", "UK", "Germany"),
                 State = c("NY", "London", "Bavaria", "SP", "CA", "RJ", "LONDON", "Berlin"),
                 Candidate = c(1:8))
print(df)

output

  Country   State Candidate
1      US      NY         1
2      UK  London         2
3 Germany Bavaria         3
4  Brazil      SP         4
5      US      CA         5
6  Brazil      RJ         6
7      UK  LONDON         7
8 Germany  Berlin         8

然后運行以下代碼將 London 替換為 State 等於“LONDON”的所有實例

df[df$State == "LONDON", "State"] <- "London"

現在 output 將是

 Country   State Candidate
1      US      NY         1
2      UK  London         2
3 Germany Bavaria         3
4  Brazil      SP         4
5      US      CA         5
6  Brazil      RJ         6
7      UK  London         7
8 Germany  Berlin         8

暫無
暫無

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

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