- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check which element of a list which is a logical list is TRUE in R?
To check which element of a list which is a logical list is TRUE, we can follow the below steps −
- First of all, create a list with logical values.
- Use which function with unlist function to find the position of TRUE elements.
Create the list
Let’s create a list as shown below −
List<- list(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE,FALSE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE) List
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[[1]] [1] TRUE [[2]] [1] FALSE [[3]] [1] TRUE [[4]] [1] TRUE [[5]] [1] FALSE [[6]] [1] FALSE [[7]] [1] FALSE [[8]] [1] TRUE [[9]] [1] FALSE [[10]] [1] TRUE [[11]] [1] FALSE [[12]] [1] FALSE [[13]] [1] TRUE [[14]] [1] TRUE [[15]] [1] TRUE [[16]] [1] TRUE [[17]] [1] FALSE [[18]] [1] TRUE [[19]] [1] FALSE [[20]] [1] FALSE [[21]] [1] TRUE [[22]] [1] FALSE [[23]] [1] TRUE [[24]] [1] FALSE [[25]] [1] TRUE
Find the position of TRUE elements
Unlist of the List elements and use which function to find which elements are TRUE in List −
List<- list(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE,FALSE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE) < which(unlist(List))
Output
[1] 1 3 4 8 10 13 14 15 16 18 21 23 25
Advertisements