I have a dataframe with a dimension column and 4 value columns. How can I subset the column such that all 4 columns for each record are less than a given x? I know I could d
You can use the negated rowSums()
for the subset
df[!rowSums(df[-1] > 0.7), ]
# zips ABC DEF GHI JKL
# 4 4 0.6 0.4 0.2 0.3
# 6 6 0.2 0.7 0.3 0.4
df[-1] > 0.7
gives us a logical matrix telling us which df[-1]
are greater than 0.7rowSums()
sums across those rows (each TRUE value is equal to 1, FALSE is zero)!
converts those values to logical and negates them, so that we get any row sums which are zero (FALSE) and turn them into TRUE. In other words, if the rowSums()
result is zero, we want those rows.Another way to get the same logical vector would be to do
rowSums(df[-1] > 0.7) == 0