How to extract every umpteenth element from a list?

Asked

Viewed 158 times

4

Considering a list composed 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"))

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

I tried to access them through the operators [] and [[]], 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 analyses I need to perform are simple, one of them is to know in which position the maximum value between the ALPHA values (I thought about the function which.max()).

The other analyses are at the same level and will be possible only with the manipulation of the solution of this case.

  • 2

    Just to explain why your attempt didn’t work: The operator [[ can only be used to return an object element, because it returns an object of the same element type (vector, in your case), while the operator [ always returns an object of the same type as the object on which it is applied, even if it has only one element. This may be a bit confusing, but you will see the difference if you compare the results of lista[[1]] and lista[1] using the functions str, class and typeof.

  • Thanks for the @Molx! explanations Very helpful and relevant.

1 answer

4


You can use base p/ 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 package purrr with the syntax a little more concise:

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

P/ find the index of the list that has the largest alfa, you can use the function detect_index still of the package purrr:

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

Or take the integer vector in which the highest 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"
  • 1

    Thanks Daniel. With this I avoid a tie. I was already satisfied with the first version, but with the edition was better. Thanks!

  • 1

    Just be careful that the result is a vector of characters and if you have e.g. nº 100 and 11, R will consider 100 < 11. You may need to convert p/ numeric before

  • 1

    Copy! I’m making the conversion.

  • 2

    @Jean If you want to further simplify the first option, you can avoid the function declaration and use the following: sapply(lista, "[", 3)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.