代码之家  ›  专栏  ›  技术社区  ›  Adrian

R: 如何将数据帧除了给定的列

  •  0
  • Adrian  · 技术社区  · 4 年前
    mydat <- head(iris)
    > mydat
      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
    6          5.4         3.9          1.7         0.4  setosa
    

    col = "Petal.Width" . 也就是说,我希望输出看起来像

      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1            0           0            0         0.2       0
    2            0           0            0         0.2       0
    3            0           0            0         0.2       0
    4            0           0            0         0.2       0
    5            0           0            0         0.2       0
    6            0           0            0         0.4       0
    

    1 回复  |  直到 4 年前
        1
  •  0
  •   Ronak Shah    4 年前

    删除 col names mydat 并将所有其他值替换为0。

    col = "Petal.Width"
    mydat[setdiff(names(mydat), col)] <- 0
    mydat
    
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #1            0           0            0         0.2       0
    #2            0           0            0         0.2       0
    #3            0           0            0         0.2       0
    #4            0           0            0         0.2       0
    #5            0           0            0         0.2       0
    #6            0           0            0         0.4       0
    

    dplyr across 比如:

    library(dplyr)
    mydat %>% mutate(across(-all_of(col), ~0))
    
        2
  •  0
  •   holdmygruyere    4 年前

    一种方法:

    x <- 1:5
    y <- 1:5
    z <- 1:5
    d <-data.frame(x,y,z)
    
    d[,-2] <- 0 #zero out all columns but column 2
    d
    x y z
    0 1 0
    0 2 0
    0 3 0
    0 4 0
    0 5 0
    
        3
  •  0
  •   Kian    4 年前

    你也可以试试这个:

    head(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          5.0         3.6          1.4         0.2  setosa
    6          5.4         3.9          1.7         0.4  setosa
    
    iris[,-4] <- 0
     head(iris)
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1            0           0            0         0.2       0
    2            0           0            0         0.2       0
    3            0           0            0         0.2       0
    4            0           0            0         0.2       0
    5            0           0            0         0.2       0
    6            0           0            0         0.4       0