Repeat vector to fill down column in data frame

后端 未结 2 954
陌清茗
陌清茗 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:36

    If the vector can be evenly recycled, into the data.frame, you do not get and error or a warning:

    df <- data.frame(x = 1:10)
    df$z <- 1:5
    

    This may be what you were experiencing before.

    You can get your vector to fit as you mention with rep_len:

    df$y <- rep_len(1:3, length.out=10)
    

    This results in

    df
        x z y
    1   1 1 1
    2   2 2 2
    3   3 3 3
    4   4 4 1
    5   5 5 2
    6   6 1 3
    7   7 2 1
    8   8 3 2
    9   9 4 3
    10 10 5 1
    

    Note that in place of rep_len, you could use the more common rep function:

    df$y <- rep(1:3,len=10)
    

    From the help file for rep:

    rep.int and rep_len are faster simplified versions for two common cases. They are not generic.

提交回复
热议问题