how to get the line number from the list and write it to a vector

I have a list in which the rows from the data frame are stored.

kl <- list(structure(list(GROUP = "1", col6 = NA_character_, col7 = NA_character_, 
    dif = NA_character_, p.value = NA_character_), row.names = 1L, class = "data.frame"), 
    structure(list(GROUP = "2", col6 = NA_character_, col7 = NA_character_, 
        dif = NA_character_, p.value = NA_character_), row.names = 3L, class = "data.frame"), 
    structure(list(GROUP = "3", col6 = NA_character_, col7 = NA_character_, 
        dif = NA_character_, p.value = NA_character_), row.names = 5L, class = "data.frame"), 
    structure(list(GROUP = "4", col6 = NA_character_, col7 = NA_character_, 
        dif = NA_character_, p.value = NA_character_), row.names = 7L, class = "data.frame"))

how to get the line number from the list and write it to a vector

kl.vec <- c(1,3,5,7)

>Solution :

Test data as below:

kl <- list(structure(list(GROUP = "1",  row.names = 1, class = "data.frame")), 
           structure(list(GROUP = "2",  row.names = 3, class = "data.frame")), 
           structure(list(GROUP = "3",  row.names = 5, class = "data.frame")), 
           structure(list(GROUP = "4",  row.names = 7, class = "data.frame")))

then we can do

sapply(c(1:length(kl)), FUN=function(x) kl[[x]]$row.names)

it returns

1 3 5 7

Leave a Reply