Select element in list R

R ProgrammingServer Side ProgrammingProgramming

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_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"

Published on 11-Aug-2020 06:22:30

utils [version 3.6.2]

Select item[s] from a character vector.

select.list[choices, preselect = NULL, multiple = FALSE, title = NULL, graphics = getOption["menu.graphics"]]

a character vector of items.

a character vector, or NULL. If non-null and if the string[s] appear in the list, the item[s] are selected initially.

logical: can more than one item be selected?

optional character string for window title, or NULL for no title.

logical: should a graphical widget be used?

A character vector of selected items. If multiple is false and no item was selected [or Cancel was used], "" is returned. If multiple is true and no item was selected [or Cancel was used] then a character vector of length 0 is returned.

The normal default is graphics = TRUE.

On Windows,

this brings up a modal dialog box with a [scrollable] list of items, which can be selected by the mouse. If multiple is true, further items can be selected or deselected by holding the control key down whilst selecting, and shift-clicking can be used to select ranges.

Normal termination is via the ‘OK’ button or by hitting Enter or double-clicking an item. Selection can be aborted via the ‘Cancel’ button or pressing Escape.

Under the macOS GUI,

this brings up a modal dialog box with a [scrollable] list of items, which can be selected by the mouse.

On other Unix-like platforms

it will use a Tcl/Tk listbox widget if possible.

If graphics is FALSE or no graphical widget is available it displays a text list from which the user can choose by number[s]. The multiple = FALSE case uses menu. Preselection is only supported for multiple = TRUE, where it is indicated by a "+" preceding the item.

It is an error to use select.list in a non-interactive session.

menu, tk_select.list for a graphical version using Tcl/Tk.

# NOT RUN { select.list[sort[.packages[all.available = TRUE]]] # }

Run the code above in your browser using DataCamp Workspace

This tutorial explains how to use square brackets to extract several list elements in the R programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the example.

Creation of Exemplifying Data

The following data is used as basement for this R tutorial:

my_list

Chủ Đề