【发布时间】:2017-02-17 21:04:59
【问题描述】:
通过学习R 并跟进我之前的question 和答案,我试图弄清楚如何将sequential string 替换为data.frame 中的@987654326 @。
考虑到mtcars 数据集,我想将mtcars$hp 的值定义为(hp <100, hp >= 100 & hp <200, hp >200),分别标记为("low", "medium" and "high")。
理论上,以下顺序函数应该可以完成这项工作:
1- mtcars$hp[mtcars$hp <100] = "low"
2- mtcars$hp[mtcars$hp >=100 & mtcars$hp <200] = "medium"
3- mtcars$hp[mtcars$hp >= 200] = "high"
运行函数 1 和 2 一切顺利。
> head(mtcars, 20)
mpg cyl disp hp drat wt qsec vs am gear carb newcol
Mazda RX4 21.0 6 160.0 medium 3.90 2.620 16.46 0 1 4 4 low
Mazda RX4 Wag 21.0 6 160.0 medium 3.90 2.875 17.02 0 1 4 4 low
Datsun 710 22.8 4 108.0 low 3.85 2.320 18.61 1 1 4 1 high
Hornet 4 Drive 21.4 6 258.0 medium 3.08 3.215 19.44 1 0 3 1 low
Hornet Sportabout 18.7 8 360.0 medium 3.15 3.440 17.02 0 0 3 2 medium
Valiant 18.1 6 225.0 medium 2.76 3.460 20.22 1 0 3 1 low
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 medium
Merc 240D 24.4 4 146.7 low 3.69 3.190 20.00 1 0 4 2 high
Merc 230 22.8 4 140.8 low 3.92 3.150 22.90 1 0 4 2 high
Merc 280 19.2 6 167.6 medium 3.92 3.440 18.30 1 0 4 4 low
Merc 280C 17.8 6 167.6 medium 3.92 3.440 18.90 1 0 4 4 low
Merc 450SE 16.4 8 275.8 medium 3.07 4.070 17.40 0 0 3 3 medium
Merc 450SL 17.3 8 275.8 medium 3.07 3.730 17.60 0 0 3 3 medium
Merc 450SLC 15.2 8 275.8 medium 3.07 3.780 18.00 0 0 3 3 medium
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 medium
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 medium
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 medium
Fiat 128 32.4 4 78.7 low 4.08 2.200 19.47 1 1 4 1 high
Honda Civic 30.4 4 75.7 low 4.93 1.615 18.52 1 1 4 2 high
Toyota Corolla 33.9 4 71.1 low 4.22 1.835 19.90 1 1 4 1 high
但是,一旦我在最后两个选项之后运行函数 3 mtcars$hp[mtcars$hp >= 200] = "high",所有马力都会变成“high”!
> mtcars$hp[mtcars$hp >= 200] = "high"
> head(mtcars, 20)
mpg cyl disp hp drat wt qsec vs am gear carb newcol
Mazda RX4 21.0 6 160.0 high 3.90 2.620 16.46 0 1 4 4 low
Mazda RX4 Wag 21.0 6 160.0 high 3.90 2.875 17.02 0 1 4 4 low
Datsun 710 22.8 4 108.0 high 3.85 2.320 18.61 1 1 4 1 high
Hornet 4 Drive 21.4 6 258.0 high 3.08 3.215 19.44 1 0 3 1 low
Hornet Sportabout 18.7 8 360.0 high 3.15 3.440 17.02 0 0 3 2 medium
Valiant 18.1 6 225.0 high 2.76 3.460 20.22 1 0 3 1 low
Duster 360 14.3 8 360.0 high 3.21 3.570 15.84 0 0 3 4 medium
Merc 240D 24.4 4 146.7 high 3.69 3.190 20.00 1 0 4 2 high
Merc 230 22.8 4 140.8 high 3.92 3.150 22.90 1 0 4 2 high
Merc 280 19.2 6 167.6 high 3.92 3.440 18.30 1 0 4 4 low
Merc 280C 17.8 6 167.6 high 3.92 3.440 18.90 1 0 4 4 low
Merc 450SE 16.4 8 275.8 high 3.07 4.070 17.40 0 0 3 3 medium
Merc 450SL 17.3 8 275.8 high 3.07 3.730 17.60 0 0 3 3 medium
Merc 450SLC 15.2 8 275.8 high 3.07 3.780 18.00 0 0 3 3 medium
Cadillac Fleetwood 10.4 8 472.0 high 2.93 5.250 17.98 0 0 3 4 medium
Lincoln Continental 10.4 8 460.0 high 3.00 5.424 17.82 0 0 3 4 medium
Chrysler Imperial 14.7 8 440.0 high 3.23 5.345 17.42 0 0 3 4 medium
Fiat 128 32.4 4 78.7 high 4.08 2.200 19.47 1 1 4 1 high
Honda Civic 30.4 4 75.7 high 4.93 1.615 18.52 1 1 4 2 high
Toyota Corolla 33.9 4 71.1 high 4.22 1.835 19.90 1 1 4 1 high
知道为什么以及我做错了什么吗?
谢谢!
【问题讨论】:
-
mtcars$hp[mtcars$hp >=200 & mtcars$hp <"low"] = "high" -
@HubertL 非常感谢!请你也向我解释一下为什么以及它背后的逻辑是什么(你的功能)?
-
当您将文本(“低”)分配给 hp 时,它变为
character类型,然后您分配“中”,然后所有值都超过 200,但“中”也超过 200 和“低”也因此所有值都超过 200 并被分配为“高”。我的解决方案只是确保您不会将“高”分配给“低”和“中”的值。试试"medium" > "low"
标签: r string dataframe replace