How to filter, remove or subset rows "certain range of values" between variables?
base R
range <- apply(a_subset, 1, function(x) diff(range(x)))
a_subset[which(range <= 1),]
var1 var2 var3 var4 var5 var6
b1 0.2 0.4 0.5 -0.5 -0.3 -0.5
b3 0.5 0.3 0.1 -0.2 -0.4 -0.4
tidyr
In tidyr
, it is easier to work with tidy data:
a_subset %>%
rownames_to_column() %>%
pivot_longer(cols = -rowname) %>%
group_by(rowname) %>%
filter(diff(range(value)) <= 1) %>%
pivot_wider()