Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread Joshua Wiley
On Wed, Sep 8, 2010 at 12:02 PM, David Winsemius wrote: > > On Sep 8, 2010, at 2:24 PM, Joshua Wiley wrote: > >> Hi Jakob, >> >> You can use is.na() to create an index of which rows in column 3 are >> missing data, and then select these from column 1.  Here is a simple >> example: >> >> dat <- dat

Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread Bert Gunter
with() would seem to be useful here: m$z <- with(m,ifelse(is.na(z), x, z)) (I believe the timing is similar, but haven't checked) -- Bert On Wed, Sep 8, 2010 at 11:22 AM, Dimitris Rizopoulos wrote: > one way is the following: > > m <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))

Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread David Winsemius
On Sep 8, 2010, at 2:24 PM, Joshua Wiley wrote: Hi Jakob, You can use is.na() to create an index of which rows in column 3 are missing data, and then select these from column 1. Here is a simple example: dat <- data.frame(V1 = 1:5, V3 = c(1, NA, 3, 4, NA)) dat$new <- dat$V3 my.na <- is.na(d

Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread Joshua Wiley
Hi Jakob, You can use is.na() to create an index of which rows in column 3 are missing data, and then select these from column 1. Here is a simple example: dat <- data.frame(V1 = 1:5, V3 = c(1, NA, 3, 4, NA)) dat$new <- dat$V3 my.na <- is.na(dat$V3) dat$new[my.na] <- dat$V1[my.na] dat This sh

Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread jim holtman
?ifelse df$newCol <- ifelse(is.na(df$col3), df$col1, df$col3) On Wed, Sep 8, 2010 at 2:17 PM, Jakob Hedegaard wrote: > Hi list, > > I have a data frame (m) with 169221 rows and 10 columns and would like to > make a new column containing the content of column 3 but replace the NAs in > column 3

Re: [R] Replace NAs in one column with data from another column

2010-09-08 Thread Dimitris Rizopoulos
one way is the following: m <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100)) m$z[sample(100, 20)] <- NA m$z.new <- ifelse(is.na(m$z), m$x, m$z) I hope it helps. Best, Dimitris On 9/8/2010 8:17 PM, Jakob Hedegaard wrote: Hi list, I have a data frame (m) with 169221 rows and 10

[R] Replace NAs in one column with data from another column

2010-09-08 Thread Jakob Hedegaard
Hi list, I have a data frame (m) with 169221 rows and 10 columns and would like to make a new column containing the content of column 3 but replace the NAs in column 3 with the data in column 1 (from the same row as the NA in column 3). Column 1 has data in all rows. My first attempt was: for