0

I am trying to get this done in R. The cells in columns 2,3 and 4 contain numbers greater than 10 or 'NA'. Since this is already filtered data, I want to crosscheck with cells in columns 5,6 and 7. The datapoint in columns 5, 6 or 7 contain some alphabetical annotations. If the cells in columns 2,3 or 4 contain 'NA', then I want to convert the corresponding cell values in columns 5, 6 or 7 as 'N' or 'NA'.

My sample data is as follows:

ID S1 S2 S3 S4 S5 S6  
M1 11 20 NA  C  C  C   
M2 NA 123 21 T  T  R  
M3 NA NA 27  A  A  M  
M4 65 23 NA  G  G  C  
M5 12 NA 13  T  G  C

My desired output is::

ID S1 S2 S3 S4 S5 S6  
M1 11 20 NA  C  C  N   
M2 NA 123 21 T  N  R  
M3 NA NA 27  N  N  M  
M4 65 23 NA  G  G  N  
M5 12 NA 13  T  N  C

Thanks in advance. Jerry

2
  • Just checking--the second line of your desired output is wrong, correct? Commented Dec 2, 2015 at 2:24
  • yes you are correct. i think it should be N T R Commented Dec 2, 2015 at 22:30

1 Answer 1

1

You could try this:

library(data.table)
data <- as.data.table(list(ID=c("M1","M2"), S1=c(11,NA), s4=c("C", "T")))

#   ID S1 s4
#1: M1 11  C
#2: M2 NA  T

data[, s4 := ifelse(is.na(S1), NA, s4)]

#   ID S1 s4
#1: M1 11  C
#2: M2 NA NA
Sign up to request clarification or add additional context in comments.

2 Comments

how to write the same thing for 200 x 200 datatable.
Search for apply a function on multiple columns in R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.