I have the following simplified dataframe.
test <- data.frame(
ice = c(1, 0.8, 0.5, 0.4),
eonia = c(0.5, 0, 0, -0.4),
euribor = c(1, -0.8, 1, -0.2),
cp = c(-0.7, -0.6, -0.4, -0.5)
)
row.names(test) <- colnames(test)
I would like to apply a condition for every column, which keeps only those values satisfying the condition:
test[(test$ice>= 0.8 & test$ice< 1) | (test$ice<= -0.8 & test$ice> -1), , drop=FALSE]
However, my real dataframe contains of many variables and I dont want to apply this code "manually" to every column. Note that I might need to add each column to a list or a new dataframe after filtering for this condition.
Is there an efficient way to loop over every column and maybe safe every column as a new dataframe or add it to a list.
The first dataframe (or part of the list) should look like this:
ice
ice 1
eonia 0.8
Many thanks in advance