﻿ Repeat Vector to Fill Down Column in Data Frame - ITCodar

# Repeat Vector to Fill Down Column in Data Frame

## Repeat vector to fill down column in data frame

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 y1   1 1 12   2 2 23   3 3 34   4 4 15   5 5 26   6 1 37   7 2 18   8 3 29   9 4 310 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.

## Mutate a column counting only 1 - 2 - 3 - 1 - 2 - 3

If number of rows is exactly divisible by the length of the vector then you can use :

``df\$row <- 1:3``

A general solution would be to repeat `1:3` till number of rows in the dataframe.

``df\$row <- rep(1:3, length.out = nrow(df))``

## Fill data frame by column with for loop

### Solution

``L_df <- data.frame(sapply(pm, function(x) x * L * ((w - wu) / 100)))names(L_df) <- c("L_por0", "L_por1", "L_por2", "L_por3", "L_por4", "L_por5",                 "L_por6", "L_por7", "L_por8", "L_por9", "L_por10")L_df L_por0    L_por1    L_por2    L_por3    L_por4    L_por5    L_por6    L_por71   1735.24  1717.888  1700.535  1683.183  1665.830  1648.478  1631.126  1613.7732   3470.48  3435.775  3401.070  3366.366  3331.661  3296.956  3262.251  3227.5463   5205.72  5153.663  5101.606  5049.548  4997.491  4945.434  4893.377  4841.3204   6940.96  6871.550  6802.141  6732.731  6663.322  6593.912  6524.502  6455.0935   8676.20  8589.438  8502.676  8415.914  8329.152  8242.390  8155.628  8068.8666  10411.44 10307.326 10203.211 10099.097  9994.982  9890.868  9786.754  9682.6397  12146.68 12025.213 11903.746 11782.280 11660.813 11539.346 11417.879 11296.4128  13881.92 13743.101 13604.282 13465.462 13326.643 13187.824 13049.005 12910.1869  15617.16 15460.988 15304.817 15148.645 14992.474 14836.302 14680.130 14523.95910 17352.40 17178.876 17005.352 16831.828 16658.304 16484.780 16311.256 16137.73211 19087.64 18896.764 18705.887 18515.011 18324.134 18133.258 17942.382 17751.50512 20822.88 20614.651 20406.422 20198.194 19989.965 19781.736 19573.507 19365.27813 22558.12 22332.539 22106.958 21881.376 21655.795 21430.214 21204.633 20979.05214 24293.36 24050.426 23807.493 23564.559 23321.626 23078.692 22835.758 22592.82515 26028.60 25768.314 25508.028 25247.742 24987.456 24727.170 24466.884 24206.598      L_por8    L_por9   L_por101   1596.421  1579.068  1561.7162   3192.842  3158.137  3123.4323   4789.262  4737.205  4685.1484   6385.683  6316.274  6246.8645   7982.104  7895.342  7808.5806   9578.525  9474.410  9370.2967  11174.946 11053.479 10932.0128  12771.366 12632.547 12493.7289  14367.787 14211.616 14055.44410 15964.208 15790.684 15617.16011 17560.629 17369.752 17178.87612 19157.050 18948.821 18740.59213 20753.470 20527.889 20302.30814 22349.891 22106.958 21864.02415 23946.312 23686.026 23425.740``

### Explanation

The `sapply()` function can be used to iterate over vectors in a more idiomatic way for R programming. We iterate over `pm` and use your formula once since R is vectorised; each time it creates a vector of length 15 (so 11 vectors of length 15), and when we wrap it in `data.frame()` returns the data frame you want and we add in the column names.

NOTE: Applying functions to every element of a vector using an `apply()` family function has some different implications than iterating using for loops. In your case, I think `sapply()` is easier and more understandable. For more information on when you need a loop or when something like apply is better, see for example this discussion from Hadley Wickham's Advanced R book.

## Repeat rows of a data.frame

``df <- data.frame(a = 1:2, b = letters[1:2]) df[rep(seq_len(nrow(df)), each = 2), ]``

## Repeat rows of a data.frame N times

EDIT: updated to a better modern R answer.

You can use `replicate()`, then `rbind` the result back together. The rownames are automatically altered to run from 1:nrows.

``d <- data.frame(a = c(1,2,3),b = c(1,2,3))n <- 3do.call("rbind", replicate(n, d, simplify = FALSE))``

A more traditional way is to use indexing, but here the rowname altering is not quite so neat (but more informative):

`` d[rep(seq_len(nrow(d)), n), ]``

Here are improvements on the above, the first two using `purrr` functional programming, idiomatic purrr:

``purrr::map_dfr(seq_len(3), ~d)``

and less idiomatic purrr (identical result, though more awkward):

``purrr::map_dfr(seq_len(3), function(x) d)``

and finally via indexing rather than list apply using `dplyr`:

``d %>% slice(rep(row_number(), 3))``

## Adding a column to data.frame and fill that column with a particular string using R

We can just assign the 'z'

``df1\$x1 <- 'z'``

and then change the order of the columns,

``df1[c(1, 6, 2:5)]``

Or if this column needs to be created in a particular position, we can use `append`

``data.frame(append(df1, c(x1='z'), after=1))#  y1 x1 x2 x3 x4 x5#1  2  z  3  5  2  5#2 11  z 13 34  4  4#3 21  z  3 45 55 89``

## How to fill in rows with repeating data in pandas?

Seems there is no elegant way. This is the workaround I just figured out. Basically create a repeating list just bigger than original dataframe, and then left join them.

``import pandasdf = pandas.DataFrame(range(100), columns=['first'])repeat_arr = [1, 2, 3]df = df.join(pandas.DataFrame(repeat_arr * (len(df)/len(repeat_arr)+1),    columns=['second']))``

## Repeat Rows in Data Frame n Times

Use a combination of `pd.DataFrame.loc` and `pd.Index.repeat`

``test.loc[test.index.repeat(test.times)]  id  times0  a      20  a      21  b      31  b      31  b      32  c      13  d      53  d      53  d      53  d      53  d      5``

To mimic your exact output, use `reset_index`

``test.loc[test.index.repeat(test.times)].reset_index(drop=True)   id  times0   a      21   a      22   b      33   b      34   b      35   c      16   d      57   d      58   d      59   d      510  d      5``

## How to use group_by() with rep_len() r

The dplyr package has a count function `n()` which could work.

``mtcars %>%     group_by(cyl) %>%     mutate(newcol = rep_len(1:200, length.out=n()))``

Also in the mutate statement it should be a "=" and not "<-"