[R] maintaining variable types in data frames

2009-01-22 Thread Mike Miller
Suppose X and Y are two data frames with the same structures, variable names and dimensions but with different data and different patterns of missing. I want to replace missing values in Y with corresponding values from X. I'll construct a simple two-by-two case: X -

Re: [R] maintaining variable types in data frames

2009-01-22 Thread Mike Miller
On Thu, 22 Jan 2009, Mike Miller wrote: Suppose X and Y are two data frames with the same structures, variable names and dimensions but with different data and different patterns of missing. I want to replace missing values in Y with corresponding values from X. I'll construct a simple

Re: [R] maintaining variable types in data frames

2009-01-22 Thread jim holtman
How about this: Y - as.data.frame(matrix(c(c,d,NA,4),2,2), stringsAsFactors=FALSE) X - as.data.frame(matrix(c(a,b,1,2),2,2), stringsAsFactors=FALSE) Y V1 V2 1 c NA 2 d4 X V1 V2 1 a 1 2 b 2 Y[] - lapply(seq(ncol(Y)), function(.col){ + ifelse(is.na(Y[,.col]), X[,.col],