row_duplicator: Duplicate and Split Out Rows of a dtable with Shared Values

Description Usage Arguments Examples

View source: R/row_duplicator.R

Description

Given a table and a vector of length(vector) = nrow(table), duplicate each row accordingly. Optionally, one can also split up a cell based on a string and have each substring fill a unique cell in the new rows.

Usage

1
2
row_duplicator(dtable, count.vec = NULL, split = NULL,
  split.col = NULL)

Arguments

dtable

Table (data frame, tbl, matrix) to be have rows cloned.

count.vec

Vector of counts, where 1 indicates no change, 2 indicates two copies, etc. (Does not accept 0 for dropping rows).

split

String on which to split unique values into. The result of calling str_count on split.col MUST give the same number of rows as the matching element of count.vec.

split.col

The column index (or name) containing the string in 'split'.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
dtable <- data.frame('Col1' = 1:4, 'Col2' = LETTERS[1:4])

dtable
 Col1 Col2
1    1    A
2    2    B
3    3    C
4    4    D

row_duplicator(dtable = dtable, count.vec = c(1, 2, 2, 1))
  Col1 Col2
1     1    A
2     2    B
2.1   2    B
3     3    C
3.1   3    C
4     4    D

dtable[, 2] <- c('A,B,A', 'C', 'D,E,F', 'G,H')

dtable
 Col1  Col2
1    1 A,B,A
2    2     C
3    3 D,E,F
4    4   G,H

row_duplicator(dtable = dtable, split = ',', split.col = 'Col2')
   Col1 Col2
1      1    A
1.1    1    B
1.2    1    A
2      2    C
3      3    D
3.1    3    E
3.2    3    F
4      4    G
4.1    4    H

danjamesadams/Dantools documentation built on Aug. 24, 2019, 6:15 p.m.