R dplyr变异代码可将df $ Acres从“ 10-Jan”更改为“ 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”等。
答案 0 :(得分:2)
我不确定您要在这里实现什么,但是为什么不这样保存数据框呢?
NSCache