banner



How To Access The Data From List In R

Listing in R is a data structure having elements of different data types. A Vector contains all elements of the aforementioned type is called an atomic vector, and a vector containing all elements of a dissimilar type is called List in R.

R List

 A list is a particular type of vector but tin shop mixed types. A list can incorporate different types of elements like − numbers, strings, vectors, another listing, a matrix, or fifty-fifty a part equally its elements.

How to Create a Listing in R

To create a list in r, utilise the listing() function. To cheque the type in R, use the typeof() method. To bank check the length of the List, use the length() method.

                      list_data <- list("Trump", "Biden", c(three, 11, 20), True)            impress(list_data)                  

Output

                                          Rscript Pro.R              [[1]]              [1] "Trump"              [[two]]              [1] "Biden"              [[three]]              [one] 3 11 20              [[4]]              [1] TRUE                      

To notice the structure of List in R, use the str() method.

                          list_data <- listing("Trump", "Biden", c(3, xi, 20), True)              cat(str(list_data))                      

Output

            List of 4 $ : chr "Trump" $ : chr "Biden" $ : num [1:iii] 3 11 twenty $ : logi TRUE          

You can see that each element has a different data type, unlike vector.

We have created a listing without a name tag, just nosotros can create a list with a name tag.

                          list_data <- list("X" <- "Trump", "Y" <- "Biden", "Z" <- c(iii, 11, 20), "West" = Truthful)              cat(str(list_data))                      

Output

            List of 4 $ 10: chr "Trump" $ Y: chr "Biden" $ Z: num [i:3] three 11 20 $ Due west: logi TRUE          

In this case, Ten, Y, Z, and W are chosen tags, making it easier to reference the components of the list. However, tags are optional.

Discover the length of the list.

                          list_data <- list("Trump", "Biden", c(3, 11, 20), True)              print(length(list_data))                      

Output

Find the data type of the list.

                          list_data <- list("Trump", "Biden", c(iii, 11, 20), TRUE)              print(typeof(list_data))                      

Output

How to Admission elements of the List

To admission listing elements, use vectors for indexing. The alphabetize vectors can exist Integer, logical, or grapheme.

Permit's employ the character vector every bit an index to access the get-go two elements of Listing.

                          list_data <- list("X" <- "Trump", "Y" <- "Biden", "Z" <- c(iii, eleven, twenty), "W" <- TRUE)              impress(list_data[c(one:2)])                      

Output

                          $X              [1] "Trump"              $Y              [1] "Biden"                      

You can utilise a negative integer as an index to exclude the element from the list.

                          list_data <- listing("X" <- "Trump", "Y" <- "Biden", "Z" <- c(three, 11, 20), "W" <- TRUE)              print(list_data[-2])                      

Output

                          $Ten              [1] "Trump"              $Z              [i] 3 11 20              $W              [i] TRUE                      

The list index starts from 1. Then it excludes the 2nd element from the list, as you can see from the output.

Let'south use a logical index to access list components.

                          list_data <- list("X" <- "Trump", "Y" <- "Biden", "Z" <- c(three, 11, xx), "W" <- TRUE)              print(list_data[c(TRUE, FALSE, TRUE, Imitation)])                      

Output

                          $X              [1] "Trump"              $Z              [one] 3 11 xx                      

Y'all can also access the components using the $ operator.The $ operator tin do fractional matching on tags .

                          election_date <- list("x" <- "Trump", "y" <- "Biden", "z" <- c(3, xi, 20), "w" = TRUE)              impress(election_date$z)                      

Output

To reference a listing member directly, we have to use the double foursquare subclass "[[ ]]".

How to Modify List in R

To modify a listing in R, reassign the value to a specific index of the list.

                          election <- list("Trump", "Biden", c(3, 11, 20), Truthful)              print(election)              print("Afterward changing the 2nd element")              election[two] <- "Kamala"              print(election)                      

Output

                          [[1]]              [1] "Trump"              [[two]]              [i] "Biden"              [[3]]              [1] 3 11 20              [[4]]              [i] True              [1] "After changing the 2nd element"              [[1]]              [1] "Trump"              [[2]]              [1] "Kamala"              [[3]]              [one] 3 11 20              [[4]]              [1] TRUE                      

You can run across that the 2d element of the listing is changed from Biden to Kamala.

How to Delete Element from the List in R

To remove the component in R, assignNULLto that component.

                          election <- list("Trump", "Biden", c(3, 11, 20), Truthful)              print(election)              impress("Afterwards removing the 3rd element")              election[3] <- NULL              impress(election)                      

Output

                          [[1]]              [ane] "Trump"              [[2]]              [1] "Biden"              [[3]]              [1] 3 xi twenty              [[4]]              [1] TRUE              [one] "After removing the 3rd element"              [[1]]              [ane] "Trump"              [[2]]              [1] "Biden"              [[3]]              [ane] True                      

You tin can run into that the third element is removed, and now the fourth element becomes the third element.

How to Convert R List to Vector

To convert List to Vector in R, employ the unlist()  function. The unlist() function takes the listing as input and produces a vector. All the arithmetics operations on vectors can be applied afterwards the list is converted into vector.

                          data_list <- list(1:five)              cat(str(data_list))              cat("After converting from List to Vector", "\n")              vec <- unlist(data_list)              impress(vec)                      

Output

                          List of one                              $ : int [ane:5] 1 ii three 4 5              After converting from List to Vector              [1] 1 2 3 4 5                      

Yous can see that using the unlist() method, and the list is converted into a vector.

How to Merge Lists in R

To merge the lists in R, by placing all the lists element inside one c() role.

                          num_list <- listing("10", TRUE, 11)              data_list <- listing("y", Simulated, 21)              com_list <- c(num_list, data_list)              print(com_list)                      

Output

                          [[1]]              [1] "ten"              [[2]]              [1] TRUE              [[3]]              [1] 11              [[four]]              [ane] "y"              [[5]]              [ane] Imitation              [[vi]]              [1] 21                      

You can run across from the output that the c() part is used to merge the lists.

How to Generate Lists in R

To generate a listing, use the colon operator ( : ) to generate a list of numbers.

                          num_list <- 1:vi              impress(num_list)                      

Output

R Predefined Lists

There are many predefined lists bachelor like messages, and month names are predefined.

Output

            [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "December"          

Conclusion

A list is a generic vector containing other objects. In this tutorial, nosotros have seen how to create, add, admission, update, and remove list items. You can convert the listing to vector using the unlist() method, and there are many inbuilt lists out there in R.

How To Access The Data From List In R,

Source: https://r-lang.com/r-list-create-read-update-delete/

Posted by: jacobsinen1957.blogspot.com

0 Response to "How To Access The Data From List In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel