列中的条件值更改另一列同一行中的值
Conditional value in column to change a value in same row of another column
假设我有 2 个表,对于这个例子,内置的 R 数据集 mtcars 和 iris。
我想做的是为 mtcars 中的列传递条件参数,并根据该条件更改 iris 中相应的单元格列。
例如,如果 mtcar$mpg < 20,我希望 iris$Sepal.Width 在 mtcars$mpg < 20.
的同一行中为“NA”
有:
First 5 rows of
mtcars
df1<-head(mtcars)
df1
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
First 5 rows of iris
df2<-head(iris)
df2
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
想要:
First 5 rows of
mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
First 5 rows of iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 NA 3.6 1.4 0.2 setosa
在 BASE R 中偏好 solutions/suggestions 而不是 tidyverse。
你的意思是这样的吗?
df1 <- head(mtcars, 5)
df2 <- head(iris, 5)
df2$Sepal.Length[df1$mpg < 20] <- NA
df2
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 NA 3.6 1.4 0.2 setosa
我们可以使用replace
df2$Sepal.Length <- with(df2, replace(Sepal.Length, mpg < 20, NA))
假设我有 2 个表,对于这个例子,内置的 R 数据集 mtcars 和 iris。
我想做的是为 mtcars 中的列传递条件参数,并根据该条件更改 iris 中相应的单元格列。
例如,如果 mtcar$mpg < 20,我希望 iris$Sepal.Width 在 mtcars$mpg < 20.
的同一行中为“NA”有:
First 5 rows of
mtcars
df1<-head(mtcars)
df1
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
First 5 rows of iris
df2<-head(iris)
df2
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
想要:
First 5 rows of
mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
First 5 rows of iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 NA 3.6 1.4 0.2 setosa
在 BASE R 中偏好 solutions/suggestions 而不是 tidyverse。
你的意思是这样的吗?
df1 <- head(mtcars, 5)
df2 <- head(iris, 5)
df2$Sepal.Length[df1$mpg < 20] <- NA
df2
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 NA 3.6 1.4 0.2 setosa
我们可以使用replace
df2$Sepal.Length <- with(df2, replace(Sepal.Length, mpg < 20, NA))