Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: `which` statement with multiple conditions

Tags:

r

I have a matrix which consists of 13 columns (called PCs). I want to make a new matrix including only the rows that have a value between 4 and 8 (called EUR). I tried using this statement:

EUR <- PCs[which(PCs$V13 < 9 && PCs$V13 > 3), ] 

Which unfortunately doesn't work... (I only get one row out, while there are hundreds)

Anyone knows what's wrong with this command?

like image 321
Abdel Avatar asked Feb 28 '13 16:02

Abdel


People also ask

How do I write an if statement with multiple conditions in R?

Multiple Conditions To join two or more conditions into a single if statement, use logical operators viz. && (and), || (or) and ! (not). && (and) expression is True, if all the conditions are true.

Which statement is used for multiple conditions?

Nested If Statement It is used to check the multiple conditions. This statement is like executing an if statement inside an else statement.

How do you add multiple conditions in R?

Multiple conditions can also be combined using which() method in R. The which() function in R returns the position of the value which satisfies the given condition. The %in% operator is used to check a value in the vector specified.

Which operators are used to check multiple conditions?

The SQL AND & OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two operators are called as the conjunctive operators. These operators provide a means to make multiple comparisons with different operators in the same SQL statement.


1 Answers

The && function is not vectorized. You need the & function:

EUR <- PCs[which(PCs$V13 < 9 & PCs$V13 > 3), ] 
like image 97
Ryan C. Thompson Avatar answered Nov 08 '22 05:11

Ryan C. Thompson