简体   繁体   English

从R中的3个不同向量中找出最长的长度

[英]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. 我不知道是否有此功能,但是我有3个不同长度的数据框。 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. 我想使用z作为变量,因为它的长度最长。 Is there a function that can search through these 3 variables (x,y,z) and give me back the one with the longest length? 有没有可以搜索这三个变量(x,y,z)的函数,并且还给我最长的变量?

Thanks 谢谢

We can place it in a list , use lengths to create an index of maximum length and extract those element from the list 我们可以将其放置在list ,使用lengths创建最大长度的索引,然后从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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM