Select the dataframe with the highest number of rows in a R-list

Asked

Viewed 82 times

5

Hello,

I am drawing a loop and at a certain precise moment select the dataframe with the highest number of lines within a list.

Example:

df1 <- data.frame(a = 1:15)
df2 <- data.frame(b = 1:35)
df3 <- data.frame(c = 1:105)
lista <- list(df1, df2, df3)

# resultado esperado lista[[3]]

Somehow, I wish I could say "return me the list element with higher nrow() .

Thanks for your help!

  • 1

    The other possibility is to calculate the number of lines inside the loop. Something like this: outside the loop: vec <- Numeric(). Inside the loop: vec[i] <- nrow(df1); list(df1, df2, df3)[[which.max(vec)]]

1 answer

4


I have managed to find an answer that is not ideal but serves my purposes.

#contando o número de linhas de cada dfs:
cnt <- sapply(lista, nrow)
# Agora atribuindo a um DF final:
df_final <- data.frame(lista[which.max(cnt)])
  • 6

    Note: lista[[which.max(cnt)]] is enough. Uses '[[' instead of '[' to extract the element of the list given by which.max, which is the desired df. The other way extracts a sub-list and that is why it is necessary to use data.frame() afterward.

Browser other questions tagged

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