Search code examples
rvariable-length

Finding longest length out of 3 different vectors in R


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


Solution

  • 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)