File: update.data.frame.R

package info (click to toggle)
gdata 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: sh: 27; makefile: 15
file content (35 lines) | stat: -rw-r--r-- 913 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
update.data.frame <- function(object, new, by, by.x=by, by.y=by, append=TRUE,
                              verbose=FALSE, ...)
{
  x <- object
  y <- new

  retval <- x
  x.by <- x[[by.x]]
  y.by <- y[[by.y]]

  matches.x <- match(y.by, x.by)
  matches.y <- which(!is.na(matches.x))
  nomatch.y <- which(is.na(matches.x))
  matches.x <- matches.x[!is.na(matches.x)]

  if(length(matches.x)>0)
    retval[matches.x, ] <- y[matches.y,]

  if(length(nomatch.y) && append)
    retval <- rbind(retval, y[nomatch.y,])

  if(verbose)
  {
    cat("\n")
    cat("Number of rows in x     :", nrow(x),           "\n")
    cat("Number of rows in y     :", nrow(y),           "\n")
    cat("\n")
    cat("Number of rows replaced :", length(matches.x), "\n")
    cat("Number of rows appended :", length(nomatch.y), "\n")
    cat("\n")
    cat("Number of rows in result:", nrow(retval),      "\n")
    cat("\n")
  }
  retval
}