[R] Create a new Vector based on two columns

Petr Savicky savicky at cs.cas.cz
Wed Apr 25 14:45:21 CEST 2012


On Wed, Apr 25, 2012 at 02:22:05PM +0200, Patrick Hausmann wrote:
> Hello,
> 
> I am trying to get a new vector 'x1' based on the not NA-values in 
> column 'a' and 'b'. I found a way but I am sure this is not the best 
> solution. So any ideas on how to "optimize" this would be great!
> 
> m <- factor(c("a1", "a1", "a2", "b1", "b2", "b3", "d1", "d1"),  ordered 
> = TRUE)
> df <- data.frame( a= m, b = m)
> df[1,1] <- NA
> df[4,2] <- NA
> df[2,2] <- NA
> df[6,1] <- NA
> df
> 
> w <- !apply(df, 2, is.na)
> v <- apply(w, 1, FUN=function(L) which(L == TRUE)[[1]])
> 
> for (i in 1:nrow(df) ) {
>     g[i] <- df[i, v[i]]
> }
> 
> df$x1 <- g

Hello.

The above code does not initialize g. Adding the command

  g <- rep(NA, times=nrow(df))

i get the same as using

  df$x1 <- ifelse(is.na(df$a), df$b, df$a)
  df

       a    b x1
  1 <NA>   a1  1
  2   a1 <NA>  1
  3   a2   a2  2
  4   b1 <NA>  3
  5   b2   b2  4
  6 <NA>   b3  5
  7   d1   d1  6
  8   d1   d1  6

The codes are obtained, since the original data frame contains
factors. If the intention is to keep character values, then use

  df$x1 <- ifelse(is.na(df$a), as.character(df$b), as.character(df$a))
  df

       a    b x1
  1 <NA>   a1 a1
  2   a1 <NA> a1
  3   a2   a2 a2
  4   b1 <NA> b1
  5   b2   b2 b2
  6 <NA>   b3 b3
  7   d1   d1 d1
  8   d1   d1 d1

Hope this helps.

Petr Savicky.



More information about the R-help mailing list