How to extract first value from a list in R? (2024)

How to extract first value from a list in R? (1)

  • 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

R ProgrammingServer Side ProgrammingProgramming

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

To extract first value from a list, we first need to access the element using double square brackets then the sub-element of each element will be accessed using single square brackets. For example, if we have a list called LIST containing five elements each having 10 elements then the first sub-element of the LIST will be selected by using LIST[[1]][1].

Example

Live Demo

List1<-list(x1=rnorm(50),x2=rnorm(50))List1

Output

$x1[1] 0.11130222 0.16419755 -0.52346744 2.06291702 1.75401221 1.76745535[7] 2.07647670 0.20604988 0.88090037 0.45002361 -0.41997558 0.13069306[13] 1.03792793 0.60365638 -0.86619055 -0.94122172 0.64561932 0.50256648[19] -0.61076372 0.90286990 -0.03448342 1.52897744 -0.12000996 0.07482846[25] -1.12145357 0.76479988 0.59701522 1.46033578 -0.57620173 -1.17224749[31] -0.73289297 -1.03691038 -1.28427938 -0.33767539 -1.01044720 0.22021704[37] 0.67931259 1.79052238 -0.80291917 -0.21613130 0.58112656 0.74238488[43] -0.37718519 1.31974664 2.33329258 0.33576900 -1.05233449 0.90282079[49] -0.23821323 -2.18185932$x2[1] 0.34801958 -0.24730876 -0.64642026 -0.20068985 0.94328680 -0.35876719[7] -0.65250327 -0.26434726 0.80161224 -1.98861577 -0.55784172 2.72095292[13] -0.34377048 -1.41075225 -0.58016709 0.30349424 -0.10171845 -1.63588095[19] 0.69116860 1.45962094 1.68120904 0.35287888 2.16297295 1.96956313[25] 0.20567607 0.39924676 0.18837752 -2.34209766 -0.49420627 -0.19967231[31] -0.06452823 -0.31314817 0.34765525 -0.31236504 -2.01858040 0.09302232[37] -0.07158963 0.91019112 -0.74267435 -0.40049302 0.18465365 0.65059368[43] 0.61031556 -0.23870247 -1.63562822 0.37430717 0.98758801 -0.09859013[49] 0.29618676 0.66022211

List1[[1]][1]

[1] 0.1113022

List1[[2]][1]

[1] 0.3480196

Example

Live Demo

List2<-list(y1=rpois(100,5),y2=rpois(100,5),y3=rpois(100,5),y4=rpois(100,5))List2

Output

$y1[1] 0 6 10 5 7 1 8 8 4 4 1 5 7 5 5 10 2 4 4 2 3 5 3 2 2[26] 5 4 12 4 4 5 4 4 6 6 5 7 10 1 3 9 4 6 3 7 6 4 3 6 5[51] 8 5 8 5 7 3 4 5 7 6 6 3 5 5 5 1 0 4 7 3 4 7 6 6 3[76] 3 3 4 3 5 6 4 6 3 4 4 6 7 8 4 3 10 11 6 8 8 6 6 7 4$y2[1] 7 7 11 4 4 1 4 7 2 6 2 2 6 2 2 6 3 3 6 2 5 6 6 5 6[26] 6 1 9 1 1 8 3 5 3 5 6 6 4 4 6 3 9 3 4 5 4 8 6 2 6[51] 6 2 8 8 8 5 4 1 3 7 8 6 6 10 6 8 5 2 4 7 4 8 4 3 8[76] 3 3 7 7 7 2 9 3 5 8 3 5 5 7 2 9 8 7 5 2 3 5 7 7 8$y3[1] 5 5 3 5 2 6 7 3 3 5 3 6 5 7 3 7 4 5 5 4 5 9 4 1 2[26] 4 5 5 5 5 4 3 2 6 8 4 13 6 4 6 7 5 5 6 2 3 6 4 4 6[51] 3 3 6 11 5 7 8 3 3 5 7 3 1 5 4 9 6 7 5 3 6 6 5 6 6[76] 6 6 2 12 2 1 7 3 6 4 5 4 3 4 3 5 6 4 3 8 6 7 7 7 5$y4[1] 6 7 4 6 6 1 7 6 2 6 10 7 5 9 9 3 6 6 10 4 7 4 2 4 10[26] 6 9 3 3 2 6 5 4 4 3 6 5 3 6 3 7 7 1 4 3 5 10 6 7 4[51] 5 3 6 8 3 5 4 5 5 5 6 3 4 10 5 3 7 5 5 7 4 4 3 5 6[76] 4 4 6 9 3 10 12 7 4 9 7 14 10 1 4 4 3 3 6 1 5 2 8 4 7

List2[[1]][1]

[1] 0

List2[[2]][1]

[1] 7

List2[[3]][1]

[1] 5

List2[[4]][1]

List2[[4]][1]

Example

Live Demo

List3<-list(z1=rexp(50),z2=rexp(50),z3=rexp(50),z4=rexp(50))List3

Output

$z1[1] 1.718907646 0.790585280 0.204750764 1.203047129 0.189730356 0.265698841[7] 0.471851353 1.178283257 0.278361849 1.725710894 0.004180386 1.500419515[13] 0.253964796 1.118053750 0.350007993 0.068681637 0.069928887 0.271137326[19] 0.316337873 1.513536640 0.608107488 0.100705733 0.091038117 1.680959338[25] 3.098310359 0.024069859 0.079393005 0.676481508 1.677929247 0.528531744[31] 0.952730628 0.605817827 0.552035683 0.179703399 0.029881320 1.763623732[37] 0.251440660 0.489608381 0.343958400 0.680817402 2.488113521 0.182692466[43] 1.838525970 0.026139385 1.336605331 0.187837140 1.001021679 1.068164745[49] 0.586717864 0.188288941$z2[1] 0.96681400 0.93182169 1.06428806 0.14140707 0.39303890 1.00227481[7] 0.02706789 2.80852463 1.71280020 1.03406754 1.00638814 0.70394098[13] 2.02029292 1.21125497 0.82803622 1.84601814 1.67267708 1.62547079[19] 1.43411676 1.07888560 0.26773645 1.22885541 1.12461358 0.97696734[25] 0.04547523 0.19367367 0.29813403 1.17887043 0.22968845 0.69612129[31] 0.60235370 3.02393213 2.27831540 2.85495798 0.57111993 3.15022562[37] 1.40904829 3.62419435 4.06618871 1.93677916 0.50544569 1.53942186[43] 4.00923268 0.06415687 0.16847399 0.95837914 2.17440413 0.69603180[49] 0.32872915 0.87998219$z3[1] 0.03984708 0.37911072 0.39527617 1.11247389 0.45724934 1.47759184[7] 0.90512793 0.35675484 0.88244845 0.12769840 0.00452113 0.53191616[13] 0.33870747 1.47954421 1.21998604 0.76498694 1.25436621 3.86274192[19] 0.86903165 1.02575073 3.26314757 0.96884011 2.99768499 0.73019254[25] 0.81639460 0.69500919 1.32349089 2.55732287 0.86032776 0.21881670[31] 0.23084453 0.01074831 0.48422758 0.48439722 1.38000094 0.30889465[37] 1.19599421 0.60671956 1.16873709 0.99445705 0.74635728 0.40339360[43] 1.32840395 2.92880480 1.04132163 1.68629929 0.25703192 0.68822688[49] 0.92138168 0.43555544$z4[1] 0.019785280 0.332113114 0.603268548 1.475968243 0.404461872 0.084103244[7] 0.802729027 1.684216058 2.004364217 1.100479145 0.268109553 2.453706887[13] 0.716849180 0.045104141 0.386609886 0.149478953 1.209757102 0.941370074[19] 1.726784369 0.681956061 0.427254268 0.830936060 0.692974098 0.716479172[25] 1.624493349 0.216954219 0.384070517 0.516003236 2.268504498 0.750347184[31] 2.898988536 0.022540033 0.069682582 0.324829354 0.005932369 1.836073771[37] 0.608267033 1.554355547 0.664473031 0.228425454 1.815967761 0.046082996[43] 0.865417744 0.063441784 0.015283203 0.517283282 4.257718179 0.228973207[49] 0.870923003 1.105269648

List3[[1]][1]

[1] 1.718908

List3[[2]][1]

[1] 0.966814

List3[[3]][1]

[1] 0.03984708

List3[[4]][1]

[1] 0.01978528

Nizamuddin Siddiqui

Updated on: 06-Feb-2021

4K+ Views

Related Articles

  • How to extract only first sub-element from a list in R?
  • How to extract first n values from each element in an R list?
  • How to extract first two characters from a string in R?
  • How to extract p-value and R-squared from a linear regression in R?
  • How to extract the first row of each matrix stored in a list in R?
  • How to extract the first n values of all elements of a list in R?
  • How to extract the last value of all elements of a list in R?
  • How to extract the column from a list of data frames in R?
  • How to extract correlation coefficient value from correlation test in R?
  • How to extract the p-value from t test in R?
  • How to extract the maximum value from named vector in R?
  • How to extract the first highest occurring value in an R data frame column?
  • How to extract strings based on first character from a vector of strings in R?
  • How to extract the first digit from a character column in an R data frame?
  • How to extract the last row from list of a data frame in R?
Kickstart Your Career

Get certified by completing the course

Get Started

How to extract first value from a list in R? (31)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

How to extract first value from a list in R? (2024)

FAQs

How to extract first value from a list in R? ›

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).

How do I extract a specific value from a list in R? ›

The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.

How do you find the first value in a list? ›

To get the first item you must use the index 0 . This "zero-indexing" is very common and is used by most programming languages.

How to select the first value of a variable in R? ›

To extract first value from a list, we first need to access the element using double square brackets then the sub-element of each element will be accessed using single square brackets.

How do I get the first N element of a list in R? ›

How to extract the first n values of all elements of a list in R?
  1. First of all, create a list.
  2. Then, use head function with sapply function to extract the first n values of all elements in the list.
Nov 9, 2021

How to extract the first element of a list in R? ›

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).

How do I extract a value from a list? ›

Step-by-step approach:
  1. Initialize the original list and K value.
  2. Create an empty list to store the extracted elements.
  3. Loop through the list using the enumerate() function to access both the index and value of each element in the list.
May 10, 2023

How do you find the first occurrence of a value in a list? ›

index() function is a built-in method that returns the index of the first occurrence of an item in a list. It's a simple and straightforward way to locate an item in a list. In the above snippet, my_list. index('banana') returns '1', which is the index of 'banana' in the list my_list .

How do you find the first value of an array list? ›

Get the first element of ArrayList with use of get(index) method by passing index = 0.

How to get the first element of a nested list? ›

To access values inside a nested list in Python, you can use square brackets to chain multiple index accesses together. For example, to access the first element of the second sublist in a nested list called my_list, you can use my_list[1][0].

How do I select a value from a list in R? ›

We need to use the [[index]] to select an element in a list. The value inside the double square bracket represents the position of the item in a list we want to extract. For instance, we pass 2 inside the parenthesis, R returns the second element listed.

How to select first element in data frame in R? ›

Similar to vectors and matrices, you select elements from a data frame with the help of square brackets [ ] . By using a comma, you can indicate what to select from the rows and the columns respectively. For example: my_df[1,2] selects the value at the first row and second column in my_df .

How do I select a specific value in R? ›

To limit your dataset to a subset of observations in base R, use brackets [ ] or subset() . With brackets you can subset based on row numbers, row names, or a logical expression. With subset() , you must use a logical expression.

How do you access the first element of a list? ›

Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.

How do I find one element from a list? ›

Find if an an element exists in list using filter() Function
  1. Define the list my_list and Set element_to_check.
  2. Use the filter() function to create an iterator (filtered_elements) that contains elements equal to the element_to_check.
  3. Convert the iterator filtered_elements to a list.
Mar 26, 2024

How do you pop the first element in a list? ›

The second item has an index of 1 , and so on. So, to remove the first item in a list, you specify an index of 0 as the parameter to the pop() method. And remember, pop() returns the item that has been removed.

How do I get a specific value from a list? ›

The index() function returns the index of the element in the list, and you can use this index to get the value of the element. Note that if the element does not exist in the list, the index() function will raise a ValueError .

How to extract specific values from dataframe in R? ›

Extract data frame cell value
  1. Extract value of a single cell: df_name[x, y] , where x is the row number and y is the column number of a data frame called df_name .
  2. Extract the entire row: df_name[x, ] , where x is the row number. ...
  3. Extract the entire column: df_name[, y] where y is the column number.

References

Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6118

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.