[Rd] 'data.frame' method for base::rep()
Liviu Andronic
landronimirc at gmail.com
Tue Aug 2 13:55:01 CEST 2011
Dear R developers
Would you consider adding a 'data.frame' method for the base::rep
function? The need to replicate a df row-wise can easily arise while
programming, and rep() is unable to handle such a case. See below.
> x <- iris[1, ]
> x
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
> rep(x, 2)
$Sepal.Length
[1] 5.1
$Sepal.Width
[1] 3.5
$Petal.Length
[1] 1.4
$Petal.Width
[1] 0.2
$Species
[1] setosa
Levels: setosa versicolor virginica
$Sepal.Length
[1] 5.1
$Sepal.Width
[1] 3.5
$Petal.Length
[1] 1.4
$Petal.Width
[1] 0.2
$Species
[1] setosa
Levels: setosa versicolor virginica
I found a 'rep.data.frame' function in package 'mefa' [2], but I think
it would be nice to have it in base R. In any case, the code used by
the method is very simple.
> require(mefa)
Loading required package: mefa
mefa 3.2-1 2011-05-13
> mefa:::rep.data.frame
function (x, ...)
as.data.frame(lapply(x, rep, ...))
<environment: namespace:mefa>
And here's the example above:
> rep(x, 2)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 5.1 3.5 1.4 0.2 setosa
Please le t me know what you think. Regards
Liviu
[1] http://finzi.psych.upenn.edu/R/library/mefa/html/rep.data.frame.html
[2] http://cran.at.r-project.org/web/packages/mefa/index.html
--
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
More information about the R-devel
mailing list