R使用rep()函数扩展向量
示例
该rep函数可用于以相当灵活的方式重复向量。
# repeat counting numbers, 1 through 5 twice rep(1:5, 2) [1] 1 2 3 4 5 1 2 3 4 5 # repeat vector with incomplete recycling rep(1:5, 2, length.out=7) [1] 1 2 3 4 5 1 2
每个参数对于将观察/实验单位的统计向量扩展为data.frame具有这些单位的重复观测的向量特别有用。
# same except repeat each integer next to each other rep(1:5, each=2) [1] 1 1 2 2 3 3 4 4 5 5
rep关于扩展到这样的数据结构的一个很好的功能是,可以通过将矢量替换为长度参数来指示将矢量扩展到不平衡面板,该矢量规定了重复矢量中每个元素的次数:
# automated length repetition rep(1:5, 1:5) [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 # hand-fed repetition length vector rep(1:5, c(1,1,1,2,2)) [1] 1 2 3 4 4 5 5
这应该暴露出允许外部函数提供第二个参数的可能性,rep以便动态构造根据数据扩展的向量。
与一样seq,更快,更简化的reparerep_len和版本rep.int。这些删除了一些rep保持不变的属性,因此在关注速度且不需要重复矢量的其他情况下可能最有用。
# repeat counting numbers, 1 through 5 twice rep.int(1:5, 2) [1] 1 2 3 4 5 1 2 3 4 5 # repeat vector with incomplete recycling rep_len(1:5, length.out=7) [1] 1 2 3 4 5 1 2
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短