Search code examples
rrep

invalid 'times' argument when using rep() function


I'm trying to add a column to a dataframe and I'm using the rep() function. Why do the first line work, but the next one doesn't?

> S_Grime$Græsning=rep(1:2, 50:49)
> R_Grime$Græsning=rep(1:2, 34:27)
Error in rep(1:2, 34:27) : invalid 'times' argument

My data:

> head(S_Grime)
# A tibble: 6 x 3
  Feltnummer value Græsning
       <dbl> <dbl>    <int>
1          1  6.26        1
2          2  5.72        1
3          3  8.74        1
4          4  5.33        1
5          5  6.23        1
6          6  8.67        1
> head(R_Grime)
# A tibble: 6 x 2
  Feltnummer value
       <dbl> <dbl>
1          2     2
2          3     2
3          4     2
4          5     2
5          7     2
6         11     2

> str(R_Grime)
tibble [61 x 2] (S3: tbl_df/tbl/data.frame)
 $ Feltnummer: num [1:61] 2 3 4 5 7 11 12 13 14 15 ...
 $ value     : num [1:61] 2 2 2 2 2 2 2 2 4 2 ...
> str(S_Grime)
tibble [99 x 3] (S3: tbl_df/tbl/data.frame)
 $ Feltnummer: num [1:99] 1 2 3 4 5 6 7 8 9 10 ...
 $ value     : num [1:99] 6.26 5.72 8.74 5.33 6.23 ...
 $ Græsning  : int [1:99] 1 1 1 1 1 1 1 1 1 1 ...

Solution

  • From ?rep.int:

    If times consists of a single integer, the result consists of the whole input repeated this many times. If times is a vector of the same length as x (after replication by each), the result consists of x[1] repeated times[1] times, x[2] repeated times[2] times and so on

    The behaviour if times is neither 1 nor the length of x is undefined.

    If you're expecting 34 1s, followed by 27 2s, you should do rep(1:2, c(34,27)). If you're expecting 1:2 to be recycled to the same length as 34:27 you'd need to specify that specifically with something like rep(rep(1:2, length.out=length(34:27)), 34:27)