
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select Multiple Elements of a List in R
Generally, a list in R contains a large number of elements and each element can be of different type which is a great thing about lists. Since we can store type of data as a list element therefore storage and selection to different type of data becomes easier. And we can also select single or multiple elements of the list at a time. This can be done with the help of single square brackets.
Example
Consider the below list −
> list_data <- list("India", "China", c(21,32,11), letters[1:5], TRUE, + 12, letters[-c(1:10)], c(100,101,200,201), "USA", c("Asia", "Europe"), + "Education", "Poverty", "Covid-19", 365, 12, 24, 60, 7) > list_data [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 [[4]] [1] "a" "b" "c" "d" "e" [[5]] [1] TRUE [[6]] [1] 12 [[7]] [1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[8]] [1] 100 101 200 201 [[9]] [1] "USA" [[10]] [1] "Asia" "Europe" [[11]] [1] "Education" [[12]] [1] "Poverty" [[13]] [1] "Covid-19" [[14]] [1] 365 [[15]] [1] 12 [[16]] [1] 24 [[17]] [1] 60 [[18]] [1] 7
Selecting different elements of the list_data −
> list_data[c(1,2,3)] [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 > list_data[c(18,2,3)] [[1]] [1] 7 [[2]] [1] "China" [[3]] [1] 21 32 11 > list_data[c(1,15,18)] [[1]] [1] "India" [[2]] [1] 12 [[3]] [1] 7 > list_data[c(5,10,15)] [[1]] [1] TRUE [[2]] [1] "Asia" "Europe" [[3]] [1] 12 > list_data[c(7,3,10)] [[1]] [1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[2]] [1] 21 32 11 [[3]] [1] "Asia" "Europe"
Advertisements