Anna
Anna

Reputation: 379

Conditionally set value of a column in R

I have a dataframe in R, called myTable that looks like this:

Number    Class
1          NA
4          NA
8          NA
10         NA

For each row, I want to check if the value in the Number column is greater than 5. If it is, then I'd set the Class column's entry to 1, or 0 if not. So my modified table should look like this:

Number    Class
1          0
4          0
8          1
10         1

Is there a simpler alternative than looping from 1:nrows and then checking the entry?

Thanks!

Upvotes: 0

Views: 29

Answers (1)

P. Paccioretti
P. Paccioretti

Reputation: 414

You have a very easy way:

myTable$Class <- as.numeric(myTable$Number>5)

Upvotes: 2

Related Questions