PPL
PPL

Reputation: 27

Replace value of cells across multiple columns (with values from a column) if condition is met


B1  B2  B3  total
10  10   10  30
20  20   0   40
 0  20  10   30
 0  20   0   20

I have wide dataframe with over 400 variables, using the dataframe above as a simplied example, I want to create a new dataframe with the following condition:

Going by rows, if cell in columns greater than 0, replace with value of d$total.

Output would look like this:

B1  B2  B3 
30  30  30  
40  40   0  
 0  30  30 
 0  20   0  

What is the best way to do this in R?

Thank you.

Upvotes: 1

Views: 157

Answers (2)

Vincent
Vincent

Reputation: 17725

You can use lapply and ifelse to do this:

dat <- read.table(header=TRUE, text="
B1  B2  B3  total
10  10   10  30
20  20   0   40
 0  20  10   30
 0  20   0   20")

dat <- lapply(dat, function(x) ifelse(x > 0, dat$total, x))
data.frame(dat)

#>   B1 B2 B3
#> 1 30 30 30
#> 2 40 40  0
#> 3  0 30 30
#> 4  0 20  0

Or use a loop:

for (x in colnames(dat)) {
  if (x != "total") {
    dat[[x]] <- ifelse(dat[[x]] > 0, dat$total, dat[[x]])
  }
}

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388817

You can use :

#Assuming total is the last column, removing it
d1 <- d[-ncol(d)] 
d1[] <- ifelse(d1 > 0, d$total, 0)
d1

#  B1 B2 B3
#1 30 30 30
#2 40 40  0
#3  0 30 30
#4  0 20  0

If you want to avoid ifelse you can use mathematical way to replace values which would be much faster.

d1[] <- d$total * (d1 > 0)

Upvotes: 2

Related Questions