-1

I have a data.frame like this enter image description here

I want to add Sample_Intensity_RTC and Sample_Intensity_nRTC's values and then create a new column, however in cases of Sample_Intensity_RTC and Sample_Intensity_nRTC have the same value, no addition operation is done.

Please not that these columns are not rounded in the same way, so many numbers are same with different nsmall.

Negrito
  • 220
  • 1
  • 9
  • Please include a reproducible question as suggested here [How to ask1](https://stackoverflow.com/help/minimal-reproducible-example) and [How to ask2](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) include your data (as a dataframe object or use dput("yourdata"), the code you have tried and your expected output. This will make it more likely to get a good answer.Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. – rj-nirbhay May 10 '20 at 07:40
  • Hi sir, thanks for mentioning. Here is the link https://drive.google.com/open?id=1b9-qHyI_Kg_Lm5qwPFuzBdXv3j3aKlKK – Negrito May 10 '20 at 07:49
  • Just to clarify, are the values the second row the same? How about the third row? – Edward May 10 '20 at 07:49
  • yes, they were rounded differently – Negrito May 10 '20 at 07:56
  • 1
    This is called _coalesce_ and there are many examples here on SO. – Edward May 10 '20 at 07:57
  • Awesome! Thanks! Used to work with R base, not so familiar with dplyr – Negrito May 10 '20 at 08:00
  • You could just use `ifelse`. – Edward May 10 '20 at 08:09
  • Do you want to add the two columns when the values are different in them and if they are same what do you want to do? Can you include a small part of your data here using `dput(head(data, 10))` and show expected output for that part of data? – Ronak Shah May 10 '20 at 08:26
  • Hi, @Edward gave exactly what I want. Thanks for the help! – Negrito May 10 '20 at 08:35
  • If you have the answer already please add it as an answer below so that it is helpful for the future readers. – Ronak Shah May 10 '20 at 08:40

1 Answers1

0

It seems you just want to combine these two columns, not add them in the sense of addition (+). Think of a zipper perhaps. Or two roads merging into one.

The two columns seem to have been created by two separate processes, the first looks to have more accuracy. However, after importing the data provided in the link, they have exactly the same values.

test <- read.csv("test.csv", row.names = 1)
options(digits=10)
head(test)

      Sample_ID Sample_Intensity_RTC Sample_Intensity_nRTC
1 191017QMXP002                   NA                    NA
2 191017QNXP008          41293681.00           41293681.00
3 191017CPXP009         111446376.86          111446376.86
4 191017HPXP010          92302936.62           92302936.62
5 191017USXP001                   NA           76693308.46
6 191017USXP002                   NA           76984658.00

In any case, to combine them, we can just use ifelse with the condition is.na for the first column.

test$new_col <- ifelse(is.na(test$Sample_Intensity_RTC), 
                    test$Sample_Intensity_nRTC,
                    test$Sample_Intensity_RTC)

head(test)
      Sample_ID Sample_Intensity_RTC Sample_Intensity_nRTC      new_col
1 191017QMXP002                   NA                    NA           NA
2 191017QNXP008          41293681.00           41293681.00  41293681.00
3 191017CPXP009         111446376.86          111446376.86 111446376.86
4 191017HPXP010          92302936.62           92302936.62  92302936.62
5 191017USXP001                   NA           76693308.46  76693308.46
6 191017USXP002                   NA           76984658.00  76984658.00

sapply(test, function(x) sum(is.na(x)))
            Sample_ID  Sample_Intensity_RTC Sample_Intensity_nRTC               new_col 
                    0                   126                   143                   108

You could also use the coalesce function from dplyr.

Edward
  • 10,360
  • 2
  • 11
  • 26