Adrian
Adrian

Reputation: 9793

R: how to zero-out a data.frame except a given column

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

I want to zero-out all the columns except for col = "Petal.Width". That is, I want the output to look like

  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

Is there a quick way to do this in R without writing a loop?

Upvotes: 0

Views: 90

Answers (3)

Kian
Kian

Reputation: 110

You can try this one too:

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

Upvotes: 0

holdmygruyere
holdmygruyere

Reputation: 31

One way to do it:

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

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389047

Remove col from names of mydat and replace all other values with 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

In dplyr you can use across like :

library(dplyr)
mydat %>% mutate(across(-all_of(col), ~0))

Upvotes: 0

Related Questions