user9258239
user9258239

Reputation:

Conditionally removing rows from a matrix in R

I'm working with a sizeable set of data, but before moving on with my work I'd like to remove all rows whose values in one column is less than 10.

My data.frame has two columns, each with 427 rows. Without luck, I've tried...

for (i in vals[, 1])    # vals is the name of my data frame
{                       # I want to test the condition on the values in 
                          the first column
    if (i < 10)
    {
        vals <- vals[-i, ]
    }
}

When I run my script, I get an error saying: "-i: invalid argument to unary operator"

I'm not sure if it makes a difference (I'm still very new to using R), but I'll also add that this for-loop is nested in a much bigger one. Thank you!

Upvotes: 1

Views: 4693

Answers (1)

12b345b6b78
12b345b6b78

Reputation: 1015

vals <- vals[vals[, 1] >= 10, ]

Upvotes: 2

Related Questions