Finding longest length out of 3 different vectors

2019-02-27 08:00发布

问题:

I do not know if there is a function for this but I have 3 dataframes with different lengths. I was wondering if there is a way to find which one is the largest length and load that into a variable. For example:

x <- c(1:10)
y <- c(1:20)
z <- c(1:40)

I would want to use z as my variable because it has the longest length. Is there a function that can search through these 3 variables (x,y,z) and give me back the one with the longest length?

Thanks

回答1:

We can place it in a list, use lengths to create an index of maximum length and extract those element from the list

lst[which.max(lengths(lst))]

data

lst <- list(x, y, z)


回答2:

if you have dataframe and not vectors:

lst[which.max(sapply(lst,nrow))]

data

lst <- list(df1, df2, df3)