Repeat vector to fill down column in data frame

后端 未结 2 969
陌清茗
陌清茗 2021-01-23 05:52

Seems like this very simple maneuver used to work for me, and now it simply doesn\'t. A dummy version of the problem:

df <- data.frame(x = 1:5) # create simpl         


        
2条回答
  •  半阙折子戏
    2021-01-23 06:28

    If the total number of rows is a multiple of the length of your new vector, it works fine. When it is not, it does not work everywhere. In particular, probably you have used this type of recycling with matrices:

    data.frame(1:6, 1:3, 1:4) # not a multiply
    # Error in data.frame(1:6, 1:3, 1:4) : 
    #   arguments imply differing number of rows: 6, 3, 4
    data.frame(1:6, 1:3) # a multiple
    #   X1.6 X1.3
    # 1    1    1
    # 2    2    2
    # 3    3    3
    # 4    4    1
    # 5    5    2
    # 6    6    3
    cbind(1:6, 1:3, 1:4) # works even with not a multiple
    #      [,1] [,2] [,3]
    # [1,]    1    1    1
    # [2,]    2    2    2
    # [3,]    3    3    3
    # [4,]    4    1    4
    # [5,]    5    2    1
    # [6,]    6    3    2
    # Warning message:
    # In cbind(1:6, 1:3, 1:4) :
    #   number of rows of result is not a multiple of vector length (arg 3)
    

提交回复
热议问题