R how to extract the first value from a date list.

Asked

Viewed 299 times

0

I have this list of data::

base_list <- list(structure(list(POP_84 = 17.7, POP_92 = 18.8, POP_99 = 19.7, 
                                 POP_02 = 20.5, POP_04 = 21.2, POP_09 = 23, POP_11 = 24.5), row.names = 149L, class = "data.frame"), 
                  structure(list(POP_56 = 10.8, POP_58 = 11.5, POP_60 = 12.4, 
                                 POP_66 = 14, POP_68 = 14.3, POP_71 = 16.2, POP_84 = 18.1, 
                                 POP_92 = 21.3, POP_99 = 21, POP_02 = 21.2, POP_04 = 22, 
                                 POP_09 = 25.1), row.names = 150L, class = "data.frame"), 
                  structure(list(POP_56 = 11.1, POP_58 = 11.1, POP_60 = 12.1), row.names = 151L, class = "data.frame"), 
                  structure(list(POP_56 = 11.8, POP_58 = 12.1, POP_60 = 12.1, 
                                 POP_66 = 13.1, POP_68 = 14, POP_71 = 14.6, POP_84 = 13.7, 
                                 POP_92 = 15, POP_99 = 14.5, POP_02 = 14.5, POP_04 = 14.7, 
                                 POP_09 = 14.9, POP_11 = 16.1, POP_16 = 16.2), row.names = 152L, class = "data.frame"), 
                  structure(list(POP_60 = 10.5, POP_66 = 13.1, POP_09 = 10, 
                                 POP_11 = 11.6, POP_16 = 12.6), row.names = 153L, class = "data.frame"), 
                  structure(list(POP_09 = 10.7, POP_11 = 11.2, POP_16 = 12.8), row.names = 154L, class = "data.frame"), 
                  structure(list(POP_56 = 12.4, POP_58 = 13.1, POP_60 = 13.1, 
                                 POP_66 = 14.3, POP_68 = 15, POP_71 = 15.6, POP_84 = 17.7, 
                                 POP_92 = 17.8, POP_99 = 18, POP_02 = 18.5, POP_04 = 18.8, 
                                 POP_09 = 19.1, POP_11 = 19.2, POP_16 = 19.9), row.names = 155L, class = "data.frame"), 
                  structure(list(POP_99 = 12.4, POP_02 = 13.2, POP_04 = 13.8, 
                                 POP_09 = 14.7, POP_11 = 15, POP_16 = 15.2), row.names = 156L, class = "data.frame"))

would like to extract only the first value of each data.frame of the list.

  • 2

    The displayed result makes no sense. Note that POP_56 must be 4 NA, as it appears in positions 2, 3, 4 and 7 of base_list, but has no NA in the desired result. Also, what does "I tried for the bind_rows(base_list) function mean but the result gets all messed up"? I tested the function here and the result is perfectly organized, including putting NA lines 1, 5, 6 and 8 of the final date frame.

  • I already edited the question. It really didn’t make any sense. I managed to solve the problem. Hug

2 answers

1


R is a functional language, the extraction/indexing operator can be used as a function. So, if base_list[[1]][1] extracts the first value of the first item from the list, to extract the first item from each of the elements we can use:

primeiros.valores <- lapply(base_list, "[", 1)

That is: "apply the value-per-position extraction function to the list with the argument '1'". The result is a list; it can be converted to vector with:

> unlist(primeiros.valores)
POP_84 POP_56 POP_56 POP_56 POP_60 POP_09 POP_56 POP_99 
  17.7   10.8   11.1   11.8   10.5   10.7   12.4   12.4 

0

I wanted to extract the first value from each list. I solved the problem as follows:

first created a matrix() with the desired size:

pop_list <- matrix(nrow=length(df_desejado))

then rotate this loop and extract the values:

for(i in (lin)){ # loop por linha
  pop_list[i,1] <- base_list[[i]][[1]]
}

lin is the number of lines df_desejado

Browser other questions tagged

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