Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Proper' way to do row-wise replacement

Tags:

r

I have a data frame which looks something like:

dataDemo <- data.frame(POS = 1:4 , REF = c("A" , "T" , "G" , "C") , 
    ind1 = c("A" , "." , "G" , "C") , ind2 = c("A" , "C" , "C" , "."),
                                                  stringsAsFactors=FALSE)

dataDemo

  POS REF ind1 ind2
1   1   A    A    A
2   2   T    .    C
3   3   G    G    C
4   4   C    C    .

and I'd like to replace all the "."s with the REF value for that row. Here is how I did it:

for(i in seq_along(dataDemo$REF)){
    dataDemo[i , ][dataDemo[i , ] == '.'] <- dataDemo$REF[i]
}

I'd like to know if there's a more 'proper' or idiomatic way of doing this in R. I generally try to use *apply whenever possible and this seems like something that could easily be adapted to that approach and made more readable (and run faster), but despite throwing a good bit of time at it I haven't made much progress.

like image 570
mnosefish Avatar asked Jun 20 '16 17:06

mnosefish


2 Answers

In dplyr,

library(dplyr)

dataDemo %>% mutate_each(funs(ifelse(. == '.', REF, as.character(.))), -POS)
#   POS REF ind1 ind2
# 1   1   A    A    A
# 2   2   T    T    C
# 3   3   G    G    C
# 4   4   C    C    C
like image 96
alistaire Avatar answered Sep 27 '22 23:09

alistaire


Here's another base R alternative, where we use the row numbers of the "." occurrences to replace them by the appropriate REF values.

# Get row numbers
rownrs <- which(dataDemo==".", arr.ind = TRUE)[,1]

# Replace values
dataDemo[dataDemo=="."] <- dataDemo$REF[rownrs]

# Result
dataDemo
#  POS REF ind1 ind2
#1   1   A    A    A
#2   2   T    T    C
#3   3   G    G    C
#4   4   C    C    C
like image 31
mtoto Avatar answered Sep 27 '22 22:09

mtoto