insertRows {berryFunctions} | R Documentation |
insert rows to data.frame
Description
Insert (multiple) rows to a data.frame, possibly coming from another data.frame, with value and row recycling
Usage
insertRows(df, r, new = NA, rcurrent = FALSE)
Arguments
df |
data.frame |
r |
Row number (not name!), at which the |
new |
Vector with data to be inserted, is recycled. Alternatively, a data.frame, whose rows are put into the r locations. If it has more rows than length(r), the excess rows are ignored. DEFAULT: NA |
rcurrent |
Logical: should |
Value
data.frame
Note
Has not yet been tested with RWI (really weird input), so might not be absolutely foolproof
Author(s)
Berry Boessenkool, berry-b@gmx.de, Oct 2015, based on code by Ari B. Friedmann (I added the for loop, recycling, input controls and data.framification)
References
https://stackoverflow.com/questions/11561856/add-new-row-to-dataframe
See Also
Examples
existingDF <- as.data.frame(matrix(1:20, nrow=5, ncol=4))
existingDF
insertRows(existingDF, 2) # default new=NA is recycled
insertRows(existingDF, 2, rcurrent=TRUE) # after current line, not at it
insertRows(existingDF, 2, 444:446)
insertRows(existingDF, 3, new=matrix(10:1,ncol=2)) # input warning
insertRows(existingDF, 1)
insertRows(existingDF, 5)
insertRows(existingDF, 6) # use addRows for this:
addRows(existingDF, n=1)
insertRows(existingDF, 9) # pads NA rows inbetween
# Works for multiple rows as well:
insertRows(existingDF, r=c(2,4,5), new=NA, rcurrent=TRUE)
insertRows(existingDF, r=c(2,4,5), new=NA)
insertRows(existingDF, r=c(2,4,4), new=NA)
insertRows(existingDF, r=c(2,4,4), new=NA, rcurrent=TRUE)
# Also works with a data.frame for insertion:
insertDF <- as.data.frame(matrix(101:112, nrow=3, ncol=4))
insertRows(existingDF, 3, new=insertDF) # excess rows in new are ignored
insertRows(existingDF, c(2,4,5), new=insertDF)
insertRows(existingDF, c(2,4:6), new=insertDF) # rows are recycled