[英]negative side effect on R dplyr mutate assignment to data.frame
R dplyr變異代碼可將df $ Acres從“ 10月1日”更改為“ 1到10”。 但是,一個負面影響是,當進行-> df $ Acres分配時,列名“ Acres”的其余data.frame添加了“ Acres.Acres”,“ Acres.FamilyIncome”。 可以在下面的str(df)中看到:
df %>%
mutate(Acres = case_when(Acres == "10-Jan" ~ "1 to 10",
TRUE ~ Acres)) -> df$Acres
> str(df)
'data.frame': 22745 obs. of 18 variables:
$ Acres :'data.frame': 22745 obs. of 18 variables:
..$ Acres : chr "1 to 10" "1 to 10" "1 to 10" "1 to 10" ...
..$ FamilyIncome: int 150 180 280 330 330 480 520 550 1400 1400 ...
..$ FamilyType : chr "Married" "Female Head" "Female Head" "Female Head" ...
..$ NumBedrooms : int 4 3 4 2 3 0 3 3 3 3 ...
..$ NumChildren : int 1 2 0 1 1 3 2 1 1 2 ...
..$ NumPeople : int 3 4 2 2 2 4 3 2 4 3 ...
..$ NumRooms : int 9 6 8 4 5 1 8 5 9 7 ...
..$ NumUnits : chr "Single detached" "Single detached" "Single detached" "Single detached" ...
..$ NumVehicles : int 1 2 3 1 1 0 0 1 2 1 ...
..$ NumWorkers : int 0 0 1 0 0 0 0 1 1 1 ...
..$ OwnRent : chr "Mortgage" "Rented" "Mortgage" "Rented" ...
..$ YearBuilt : chr "1950-1959" "Before 1939" "2000-2004" "1950-1959" ...
..$ HouseCosts : int 1800 850 2600 1800 860 700 270 800 710 1500 ...
..$ ElectricBill: int 90 90 260 140 150 140 130 1 150 300 ...
..$ FoodStamp : chr "No" "No" "No" "No" ...
..$ HeatingFuel : chr "Gas" "Oil" "Oil" "Oil" ...
..$ Insurance : int 2500 0 6600 0 660 0 100 0 1000 0 ...
..$ Language : chr "English" "English" "Other European" "English" ...
$ FamilyIncome: int 150 180 280 330 330 480 520 550 1400 1400 ...
$ FamilyType : chr "Married" "Female Head" "Female Head" "Female Head" ...
原始data.frame具有18個變量,在執行dplyr突變后,更新的data.frame仍具有18個變量,但是現在添加了“ Acres.Acres”,“ Acres.FamilyIncome”等。
我不確定您要在這里實現什么,但是為什么不只保存這樣的數據框呢?
df <- df %>%
mutate(Acres = case_when(Acres == "10-Jan" ~ "1 to 10",
TRUE ~ Acres))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.