Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 检查等长列表中元素的长度_R_List - Fatal编程技术网

R 检查等长列表中元素的长度

R 检查等长列表中元素的长度,r,list,R,List,检查列表中的元素是否等长 例如: l试试这个: length(unique(sapply(l, length))) == 1 # [1] FALSE 或@Pierrelaffortune的方式: length(unique(lengths(l))) == 1L 或@CathG的方式: all(sapply(l, length) == length(l[[1]])) #or all(lengths(l) == length(l[[1]])) 一些基准: #data set.seed(12

检查列表中的元素是否等长

例如:

l试试这个:

length(unique(sapply(l, length))) == 1
# [1] FALSE
或@Pierrelaffortune的方式:

length(unique(lengths(l))) == 1L
或@CathG的方式:

all(sapply(l, length) == length(l[[1]])) 
#or
all(lengths(l) == length(l[[1]]))

一些基准:

#data
set.seed(123)
l <- lapply(round(runif(1000,1,100)), runif)


library(microbenchmark)
library(ggplot2)

#benchmark
bm <-   microbenchmark(
  zx8754 = length(unique(sapply(l, length))) == 1,
  PierreLafortune=length(unique(lengths(l))) == 1L,
  CathG_1 = all(lengths(l) == length(l[[1]])),
  CathG_2 = all(sapply(l, length) == length(l[[1]])),
  times = 10000)

# result
bm
Unit: microseconds
            expr     min      lq      mean  median      uq       max neval  cld
          zx8754 326.605 355.281 392.39741 364.034 377.618 84109.597 10000    d
 PierreLafortune  23.545  25.960  30.24049  27.168  28.375  3312.829 10000  b  
         CathG_1   9.056  11.471  13.49464  12.679  13.584  1832.847 10000 a   
         CathG_2 319.965 343.207 371.50327 351.659 364.940  3531.068 10000   c 

#plot benchmark
autoplot(bm)
#数据
种子集(123)
l我会使用:

length(unique(lengths(l))) == 1L
[1] FALSE

@不能按长度(l)工作的CathG将检查列表中的桶数(即3)。@Nick right抱歉,输入错误,我的意思是第一个桶的
length
(与另一个答案相同)
length(unique(lengths(l))) == 1L
[1] FALSE