[R] split data frame temporary and work with only part of it?

Peter Ehlers ehlers at ucalgary.ca
Sun Jul 24 20:53:10 CEST 2011

On 2011-07-24 10:07, ivo welch wrote:
> dear R wizards:  I have a large data frame, a million rows, 40
> columns.  In this data frame, there are some (about 100,000) rows
> which I want to recompute (update), while I want to leave others just
> as is.  this is based on a condition that I need to compute, based on
> what is in a few of the columns.  what is the right R way to do this?
> I could subset out the rows that I want to recompute into a new data
> frame (A), subset out the rows I don't want to recompute (B), operate
> on the first data frame (A), then rbind the two (A and B) back
> together and resort into original order.  is this the recommended way?

Can't you just make an index vector of the rows that will be
changed, then make your new data frame for these rows, then
change the original data frame by indexing the rows in your
index vector?

  dat <- data.frame(x=20:1, y=sample(20))
  idx <- which(dat$y > 17)
  dat2 <- data.frame(x=c(999,-889,777), y=c(-44,NA,0))
  dat[idx, ] <- dat2

Peter Ehlers

> sincerely,
> /iaw
> ----
> Ivo Welch (ivo.welch at gmail.com)
> ______________________________________________
> 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