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
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)
if you have dataframe and not vectors:
lst[which.max(sapply(lst,nrow))]
data
lst <- list(df1, df2, df3)
来源:https://stackoverflow.com/questions/49516871/finding-longest-length-out-of-3-different-vectors-in-r