1

I'm trying to use R to merge data from different datasets using:

merge(x,y, all=T, by="ID")

After the merge occurs I obviously have the complete ID column, and the other columns full of NAs.

This is fine in general but this time I need to generate another column that retains every value of my analysis from the 3 previous columns.

    Findings.x  Findings.y
1         <NA> Decreases
2   Increases       <NA>
3         <NA> Decreases 
4   Increases       <NA>
5         <NA> Increases 
6   Increases       <NA>
7         <NA> Decreases 

The output I'm searching for is:

Findings.summary
1   Decreases
2   Increases
3   Decreases
4   Increases
5   Increases
6   Increases
7   Decreases

What is a good strategy to do this? Shall I merge them in a different way?

I'd like to keep it simply and stay into R-base if the solution is simple enough.

Edit:

This post has a similar problem, I tried to apply this solution:

data.frame(a=data$a, mycol=apply(data[,-1],1,sum,na.rm=TRUE))

But my rows are not numeric, so it doesn't work.

Edit 2

This solution of the same post solved the problem:

data$m = data$x  # your new merged column start with x
data$m[!is.na(data$y)] = data$y[!is.na(data$y)]  # merge with y
data$m[!is.na(data$z)] = data$z[!is.na(data$z)]  # merge with z

> data
  a  x  y  z m
1 A  1 NA NA 1
2 B  2 NA NA 2
3 C NA  3 NA 3
4 D NA NA  4 4
5 E NA NA  5 5

It also works on character strings, if you have a better solution let me know.

Jaap
  • 81,064
  • 34
  • 182
  • 193
luca tucciarone
  • 111
  • 1
  • 8

0 Answers0