【问题标题】:Conditional value in column to change a value in same row of another column列中的条件值以更改另一列的同一行中的值
【发布时间】:2021-09-14 11:15:35
【问题描述】:

假设我有 2 个表,对于这个例子,内置 R 数据集 mtcars 和 iris。

我要做的是为 mtcars 中的列传递条件参数,并根据该条件更改 iris 中相应的单元格列。

例如,如果 mtcar$mpg

有:


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 中的解决方案/建议而不是 tidyverse。

【问题讨论】:

    标签: r data.table data-manipulation


    【解决方案1】:

    你的意思是这样的吗?

    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
    

    【讨论】:

    • 这看起来像我想要的,我没有意识到我可以像你在这里所做的那样在多个数据集中传递条件参数。
    【解决方案2】:

    我们可以使用replace

    df2$Sepal.Length <- with(df2, replace(Sepal.Length, mpg < 20, NA))
    

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2020-05-20
      • 2022-11-04
      • 2017-04-17
      • 2022-07-27
      • 1970-01-01
      相关资源
      最近更新 更多