zoukankan      html  css  js  c++  java
  • rep()函数简介

    rep()函数:重复

    rep(x,...)

    rep.int(x,times)

    rep_len(x,length.out)

    ·x:一个向量(vector),一个因子(factor),一个POSIXct或POSIXlt或Date对象(object)...

    ·...:更多其他的选项,可能有如下的:

            ·times:必为非负整数,负数或NA是错误的。每一个向量重复的次数。

            ·length.out:必为非负整数,缺省或NA是错误的。输出向量期待的输出长度。

            ·each:必为非负整数,值为1或NA是错误的。每一个元素重复each次。

    times:同...

    length.out:非负整数,输出向量期待的输出长度。

    例1:

    > rep(1:4,2)
    [1] 1 2 3 4 1 2 3 4
    > rep(1:4,each=2)
    [1] 1 1 2 2 3 3 4 4
    > rep(1:4,c(2,2,2,2))
    [1] 1 1 2 2 3 3 4 4
    > rep(1:4,each=2,len=4)
    [1] 1 1 2 2
    > rep(1:4,each=2,len=10)
     [1] 1 1 2 2 3 3 4 4 1 1
    > rep(1:4,each=2,times=3)
     [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4

    例2:

    > rep(1,40*(1-.8))
    [1] 1 1 1 1 1 1 1
    > 40*(1-.8)
    [1] 8
    > rep(1,40*(1-.8)+1e-7)
    [1] 1 1 1 1 1 1 1 1
    > 40*(1-.8)+1e-7
    [1] 8

    例3:

    > #replicate a list
    > fred<-list(happy=1:10,name="squash")
    > rep(fred,5)
    $happy
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $name
    [1] "squash"
    
    $happy
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $name
    [1] "squash"
    
    $happy
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $name
    [1] "squash"
    
    $happy
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $name
    [1] "squash"
    
    $happy
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $name
    [1] "squash"
    

    例4:

    > x<-.leap.seconds[1:3]
    > rep(x,2)
    [1] "1972-07-01 08:00:00 CST"
    [2] "1973-01-01 08:00:00 CST"
    [3] "1974-01-01 08:00:00 CST"
    [4] "1972-07-01 08:00:00 CST"
    [5] "1973-01-01 08:00:00 CST"
    [6] "1974-01-01 08:00:00 CST"
    > rep(as.POSIXlt(x),rep(2,3))
    [1] "1972-07-01 08:00:00 CST"
    [2] "1972-07-01 08:00:00 CST"
    [3] "1973-01-01 08:00:00 CST"
    [4] "1973-01-01 08:00:00 CST"
    [5] "1974-01-01 08:00:00 CST"
    [6] "1974-01-01 08:00:00 CST"
    > rep(as.POSIXct(x),rep(2,3))
    [1] "1972-07-01 08:00:00 CST"
    [2] "1972-07-01 08:00:00 CST"
    [3] "1973-01-01 08:00:00 CST"
    [4] "1973-01-01 08:00:00 CST"
    [5] "1974-01-01 08:00:00 CST"
    [6] "1974-01-01 08:00:00 CST"
    > rep(as.Date(x),rep(2,3))
    [1] "1972-07-01" "1972-07-01" "1973-01-01"
    [4] "1973-01-01" "1974-01-01" "1974-01-01"

    例5:

    > x<-factor(LETTERS[1:4]);names(x)<-letters[1:4]
    > x
    a b c d 
    A B C D 
    Levels: A B C D
    > rep(x,2)
    a b c d a b c d 
    A B C D A B C D 
    Levels: A B C D
    > rep(x,each=2)
    a a b b c c d d 
    A A B B C C D D 
    Levels: A B C D
    > rep.int(x,2)
    [1] A B C D A B C D
    Levels: A B C D
    > rep_len(x,10)
     [1] A B C D A B C D A B
    Levels: A B C D
  • 相关阅读:
    被学长教会的高斯消元法Gauss
    KMP字符串匹配算法翔解❤
    fkwの题目(祝松松生日快乐!)
    NOI-linux下VIM的个人常用配置
    从2017年暑假到现在手打的模板↑_↑
    【テンプレート】初级数据结构
    【テンプレート】高精
    DP(第二版)
    luogu P1029 最大公约数和最小公倍数问题
    贪心题整理
  • 原文地址:https://www.cnblogs.com/wangshenwen/p/3235866.html
Copyright © 2011-2022 走看看