Finding longest length out of 3 different vectors in R

旧街凉风 提交于 2019-12-02 05:59:57

问题


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)


来源:https://stackoverflow.com/questions/49516871/finding-longest-length-out-of-3-different-vectors-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!