Reputation: 343
I have a large data frame. I need to go through and test the first row of values and remove the columns if the value in the first row isn't below a certain threshold.
red <- c(1,2,3)
blue <- c(4, 5, 4)
colors <- data.frame(red, blue)
colors <- t(colors)
colors
[,1] [,2] [,3]
red 1 2 3
blue 4 5 4
How would I, for example, keep only columns that have a red value below 2.5?
Thanks so much! Im still pretty new to this!
Upvotes: 0
Views: 158
Reputation: 61214
Try this
> colors[,colors[1, ]<2.5 ]
[,1] [,2]
red 1 2
blue 4 5
Upvotes: 2
Reputation: 60984
You could do something like:
> colors[,colors['red',] < 2.5]
[,1] [,2]
red 1 2
blue 4 5
Where you don't use an apply
function, but vectorization in stead. The expression colors['red',] < 2.5
leads to an logical vector (TRUE
, FALSE
) with TRUE
where the expression is valid, and FALSE
where it is not. You can use this logical vector to index colors
, where using colors[, logical_vector]
ensures you use the logical vector to index columns.
Upvotes: 2