How to extract all nth element from a list?

4

Considering a list consisting of several vectors:

lista<-list(c("1949", "1963", "45", "X2752009", "X2752013", "X2753007", "X2850009", "X2851005", "X2851008", "X2851014", "X2852002", "X2852003", "X2852011", "X2852016", "X2852020", "X2853005", "X2853006", "X2853008","X2854003", "X2854007", "X2854011", "X2950003", "X2950007", "X2950008", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950029", "X2950030", "X2950033", "X2950035", "X2950041", "X2951003", "X2951022", "X2951043", "X2952002", "X2952006", "X2954001", "X2956007", "X3051003", "X3051013", "X3052009", "X3053007", "X3053012"), c("1950", "1964", "49", "X2751008", "X2752009", "X2752013", "X2753007", "X2850009", "X2851005", "X2851008", "X2851014", "X2852002", "X2852003", "X2852011", "X2852016", "X2852020", "X2853005", "X2853006", "X2853008", "X2854003", "X2854007", "X2854011", "X2950003", "X2950007", "X2950008", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950029", "X2950030", "X2950033", "X2950035", "X2950041", "X2951003", "X2951022", "X2951043", "X2952002", "X2952006","X2952012", "X2952029", "X2954001", "X2956007", "X3051003", "X3051013", "X3052007", "X3052009", "X3053007", "X3053012"), c("1951", "1965", "50", "X2751008", "X2752009", "X2752013", "X2753007", "X2850009", "X2851005", "X2851008", "X2851014", "X2852002", "X2852003", "X2852011", "X2852012", "X2852014", "X2852016", "X2852020", "X2853005", "X2853006", "X2853008", "X2854003", "X2854007", "X2854011", "X2950003", "X2950007", "X2950008", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950030", "X2950033", "X2950035", "X2950041", "X2951003", "X2951022", "X2952002", "X2952012", "X2952029", "X2954001", "X2956007", "X3051003", "X3051013", "X3052003", "X3052007", "X3052009", "X3053003", "X3053007", "X3053012"), c("1952", "1966", "57", "X2751008", "X2752009", "X2752013", "X2753007", "X2850007", "X2850009", "X2851001", "X2851005", "X2851008", "X2851014", "X2851033", "X2851037", "X2852002", "X2852003", "X2852011", "X2852012", "X2852014", "X2852016", "X2852020", "X2853005", "X2853006", "X2853008", "X2854003", "X2854007", "X2854011", "X2950003", "X2950007", "X2950008", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950030", "X2950033", "X2950035", "X2950041", "X2951003", "X2951022", "X2952002", "X2952012", "X2952029", "X2953016", "X2954001", "X2956007", "X3051003","X3051013", "X3052003", "X3052007", "X3052009", "X3053005", "X3053007", "X3053012", "X3150001", "X3154001"), c("1953", "1967", "58", "X2751008", "X2752009", "X2752013", "X2753007", "X2850007", "X2850009", "X2851001", "X2851005", "X2851008", "X2851014", "X2851018", "X2851026", "X2851033", "X2852002", "X2852003", "X2852011", "X2852012", "X2852014", "X2852016", "X2852017", "X2852020", "X2853005", "X2853006", "X2853008", "X2854003", "X2854007", "X2854011", "X2950003", "X2950007", "X2950008", "X2950009", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950030", "X2950033", "X2950035", "X2950041", "X2951022", "X2952012", "X2952029", "X2953016", "X2954001", "X2956007", "X3051003", "X3052002", "X3052003", "X3052007", "X3052009", "X3053005", "X3053007", "X3053012", "X3150001", "X3154001"), c("1954", "1968", "55", "X2751008", "X2752009", "X2752013", "X2753007", "X2850007", "X2850009", "X2851001", "X2851005", "X2851008", "X2851014", "X2851018", "X2851026", "X2851033", "X2852002", "X2852003", "X2852011", "X2852014", "X2852016", "X2852017", "X2852020", "X2853006", "X2853008", "X2854003", "X2854011", "X2950003", "X2950007", "X2950008", "X2950009", "X2950010", "X2950011", "X2950014", "X2950017", "X2950018", "X2950020", "X2950024", "X2950030", "X2950033", "X2950035", "X2950041", "X2951022", "X2952029", "X2953016", "X2954001", "X2956007", "X3051003", "X3052002", "X3052003", "X3052007", "X3052008", "X3052009", "X3053005", "X3053007", "X3053012", "X3150001", "X3154001"))

This is a test result, of which I need to analyze the third element of all vectors in this list. To facilitate understanding I will call them ALFA values.

I tried to access them using the [] and [[]] operators, but got the following error message when applying the function below:

lista[[1:6]][3]
Error in lista[[1:6]] : recursive indexing failed at level 2

The analyzes that I need to perform are simple, one of them is to know the position of the maximum value between the ALFA values (I thought of the which.max() function).

The rest of the analysis follows the same level and will only be possible with the manipulation of the solution of this case.

    
asked by anonymous 23.05.2016 / 17:06

1 answer

4

You can use base to get the third element of each vector:

> sapply(lista, function(x) x[3])
[1] "45" "49" "50" "57" "58" "55"

The same thing you can do using the purrr package with the slightly more concise syntax:

library(purrr)
> map_chr(lista, ~.x[3])
[1] "45" "49" "50" "57" "58" "55"

To find the index of the list that has the highest alfa , you can use the detect_index function of the purrr package as well:

> alfa <- map_chr(lista, ~.x[3])
> detect_index(lista, ~.x[3] == max(alfa))

Or get the integer vector with the largest value of alfa :

> keep(lista, ~.x[3] == max(alfa))
[[1]]
 [1] "1953"     "1967"     "58"       "X2751008" "X2752009" "X2752013" "X2753007"
 [8] "X2850007" "X2850009" "X2851001" "X2851005" "X2851008" "X2851014" "X2851018"
[15] "X2851026" "X2851033" "X2852002" "X2852003" "X2852011" "X2852012" "X2852014"
[22] "X2852016" "X2852017" "X2852020" "X2853005" "X2853006" "X2853008" "X2854003"
[29] "X2854007" "X2854011" "X2950003" "X2950007" "X2950008" "X2950009" "X2950010"
[36] "X2950011" "X2950014" "X2950017" "X2950018" "X2950020" "X2950024" "X2950030"
[43] "X2950033" "X2950035" "X2950041" "X2951022" "X2952012" "X2952029" "X2953016"
[50] "X2954001" "X2956007" "X3051003" "X3052002" "X3052003" "X3052007" "X3052009"
[57] "X3053005" "X3053007" "X3053012" "X3150001" "X3154001"
    
23.05.2016 / 19:19