美文网首页生物信息分析:从入门到精通
R语言:lengths计算列表list中元素的个数

R语言:lengths计算列表list中元素的个数

作者: Bioconductor | 来源:发表于2016-12-09 21:19 被阅读2444次

    本文首发为CSDN博客,地址为:http://blog.csdn.net/xxzhangx/article/details/53544185

    欢迎关注,谢谢!引用转载请注明作者和地址!

    这次介绍一个大家可能会用到的函数,但是又与我们常用的函数有所区别。

    lengths

    一看,还以为是length函数,我们要介绍的函数后面多了一个s,专门用于计算列表list的长度,下面用例子来呈现我们的函数。

    首先,我们先来介绍length函数,再来介绍lengths,最后对两个差异的地方进行对比,查看区别。

    length

    Get or set the length of vectors (including lists) and factors, and of any other R object for which a method has been defined

    帮助文档内容:x是向量,因子,列表

    > length(diag(4))  # = 16 (4 x 4)
    [1] 16
    > length(1:5)
    [1] 5
    > length(options())  # 12 or more
    [1] 76
    > length(y ~ x1 + x2 + x3)  # 3
    [1] 3
    > length(expression(x, {y <- x^2; y+2}, x^y))  # 3
    [1] 3
    > require(stats)
    > fm1 <- lm(breaks ~ wool * tension, data = warpbreaks)
    > length(fm1$call)      # 3, lm() and two arguments.
    [1] 3
    > length(formula(fm1))  # 3, ~ lhs rhs
    [1] 3
    

    lengths

    Get the length of each element of a list or atomic vector (is.atomic) as an integer or numeric vector.

    x为列表、向量

    > require(stats)
    > l <- split(airquality$Ozone, airquality$Month)
    > l
    $`5`
     [1]  41  36  12  18  NA  28  23  19   8  NA   7  16  11  14  18  14  34   6  30
    [20]  11   1  11   4  32  NA  NA  NA  23  45 115  37
    
    $`6`
     [1] NA NA NA NA NA NA 29 NA 71 39 NA NA 23 NA NA 21 37 20 12 13 NA NA NA NA NA
    [26] NA NA NA NA NA
    
    $`7`
     [1] 135  49  32  NA  64  40  77  97  97  85  NA  10  27  NA   7  48  35  61  79
    [20]  63  16  NA  NA  80 108  20  52  82  50  64  59
    
    $`8`
     [1]  39   9  16  78  35  66 122  89 110  NA  NA  44  28  65  NA  22  59  23  31
    [20]  44  21   9  NA  45 168  73  NA  76 118  84  85
    
    $`9`
     [1] 96 78 73 91 47 32 20 23 21 24 44 21 28  9 13 46 18 13 24 16 13 23 36  7 14
    [26] 30 NA 14 18 20
    > lengths(l,use.names = F)
    [1] 31 30 31 31 30
    > lengths(expression(u, v, 1+ 0:9))
    [1] 1 1 3
    > f <- c(rep(1, 3), rep(2, 6), 3)
    > f
     [1] 1 1 1 2 2 2 2 2 2 3
    > dates <- split(as.POSIXlt(Sys.time() + 1:10), f)
    > dates
    $`1`
    [1] "2016-12-09 21:09:50 CST" "2016-12-09 21:09:51 CST"
    [3] "2016-12-09 21:09:52 CST"
    
    $`2`
    [1] "2016-12-09 21:09:53 CST" "2016-12-09 21:09:54 CST"
    [3] "2016-12-09 21:09:55 CST" "2016-12-09 21:09:56 CST"
    [5] "2016-12-09 21:09:57 CST" "2016-12-09 21:09:58 CST"
    
    $`3`
    [1] "2016-12-09 21:09:59 CST"
    > lengths(dates,use.names=F)
    [1] 3 6 1
    
    

    lengths 和 length

    > f <- c(rep(1, 3), rep(2, 6), 3)
    > f
     [1] 1 1 1 2 2 2 2 2 2 3
    > dates <- split(as.POSIXlt(Sys.time() + 1:10), f)
    > dates
    $`1`
    [1] "2016-12-09 21:09:50 CST" "2016-12-09 21:09:51 CST"
    [3] "2016-12-09 21:09:52 CST"
    
    $`2`
    [1] "2016-12-09 21:09:53 CST" "2016-12-09 21:09:54 CST"
    [3] "2016-12-09 21:09:55 CST" "2016-12-09 21:09:56 CST"
    [5] "2016-12-09 21:09:57 CST" "2016-12-09 21:09:58 CST"
    
    $`3`
    [1] "2016-12-09 21:09:59 CST"
    > lengths(dates,use.names=F)
    [1] 3 6 1
    > length(dates)
    [1] 3
    

    length得到的是list的个数,lengths是得到每个list下的个数。这个是计算时很重要。顿时感觉这个函数很神奇。哈哈哈!

    相关文章

      网友评论

        本文标题:R语言:lengths计算列表list中元素的个数

        本文链接:https://www.haomeiwen.com/subject/rtdymttx.html