[R] Extending data frame with longer columns
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Feb 17 14:12:23 CET 2010
If your data frame only has numeric entries you could represent it as
a multivariate ts time series in which case this works:
> xx <- ts(cbind(a = 1:2, b = 3:4, c = 5:6)); xx
Time Series:
Start = 1
End = 2
Frequency = 1
a b c
1 1 3 5
2 2 4 6
> cbind(a = ts(1:4), b = xx[, "b"], c = xx[, "c"])
Time Series:
Start = 1
End = 4
Frequency = 1
a b c
1 1 3 5
2 2 4 6
3 3 NA NA
4 4 NA NA
It can be done with the sqldf package like this:
> library(sqldf)
> DF <- data.frame(a = 1:5, b = 6:10)
> DFnew <- data.frame(a = 1:7)
> sqldf("select DFnew.a, DF.b from DFnew left join DF on DF.rowid = DFnew.rowid")
a b
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
6 6 NA
7 7 NA
On Wed, Feb 17, 2010 at 4:00 AM, Ralf B <ralf.bierig at gmail.com> wrote:
> Hi,
>
> I am a beginner in R and have only read a few chapters in the R book,
> I was not able to find a solution for this simple problem.
>
> I have an empty data frame:
>
> a=data.frame(name="test")
>
> which I would like to extend in a for-loop (with data extracted from a
> database). Ideally I would like to extend the data frame like this:
>
> a["new_1"] = 1:10
> a["new_1"] = 1:12
> a["new_1"] = 1:14
>
> R now obviously complains about the changing length of the new
> columns. However, I would like to have missing values being added
> whenever columns are shorter than a newer (longer) column. How can I
> do that?
>
> Thanks,
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list