1

here is my data frame:

ID A B C D E F 
1  0 1 3 5 4 2
2  0 0 0 0 1 0
3  1 2 3 4 4 2
4  0 0 1 1 0 0

I want to get a new data frame based on row sums, if row sums<10, all value in this row changed to 0, which should be like:

ID A B C D E F 
1  0 1 3 5 4 2
2  0 0 0 0 0 0
3  1 2 3 4 4 2
4  0 0 0 0 0 0

is there any easy way to make it?

Feixiang Sun
  • 117
  • 6

1 Answers1

0

You can use rowSums as :

df[rowSums(df[-1], na.rm = TRUE) < 10, -1] <- 0
df

#  ID A B C D E F
#1  1 0 1 3 5 4 2
#2  2 0 0 0 0 0 0
#3  3 1 2 3 4 4 2
#4  4 0 0 0 0 0 0

-1 is to ignore the first ID column in the data from calculation of rowwise sum as well as replacing it with 0.

data

df <- structure(list(ID = 1:4, A = c(0L, 0L, 1L, 0L), B = c(1L, 0L, 
2L, 0L), C = c(3L, 0L, 3L, 1L), D = c(5L, 0L, 4L, 1L), E = c(4L, 
1L, 4L, 0L), F = c(2L, 0L, 2L, 0L)),class = "data.frame", row.names = c(NA, -4L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213