[R] Extending data frame with longer columns

David Winsemius dwinsemius at comcast.net
Wed Feb 17 14:11:43 CET 2010


On Feb 17, 2010, at 4:00 AM, Ralf B wrote:

> Hi,
>
> I am a beginner in R and have only read a few chapters in the R book,

Which "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")

No, you have a dataframe with one column of type "character" and one  
row:
 > a
   name
1 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

You cannot use indexing to "extend" dataframes. Dataframes are not  
appropriate for arbitrarily long data objects. Lists are better for  
that purpose.

>
> 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.

You can wish for anything, but this is not the default behavior of  
dataframes. Under normal circumstances adding a shorter vector will  
result in "argument recycling", so to circumvent that you will need to  
add the appropriate length of NAs to the end of your vectors:

dtfrm$new <- c(vec, rep(NA, length(vec)- nrow(dtfrm))
# can't use length dtfrm since that is number of columns
# is this like telomeres on chromosomes?

Add adding longer vector than the number of rows with "$ <- "  
assignment will cause an error;

You could pre-dimension your dataframe so it is ready to hold your data:

needed_rows <- 1000
 > dtfrm <- data.frame(dummy=rep(NA, needed_rows) )
 > str(dtfrm)
'data.frame':	1000 obs. of  1 variable:
  $ dummy: logi  NA NA NA NA NA NA ...

If you then need to add complete rows of new data, you can also use  
rbind. You probably ought to look at the mechanisms for accessing  
databases directly. Possible search terms: sqlite, sqldf, RODBC


> How can I
> do that?

Read the help pages:

?"["
?rbind
?cbind  # may be the same

>
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list