1

I Have three vectors. Each vector stating list of participants have that disease.

A.Cancer= c(Part1,Part2,Part3,Part4)
B.Cancer= c(Part3,Part2,Part5,Part6)
C.Cancer=c(Part1,Part7,Part8,Part9,Part10)

I want to combine them in a way that no repetition.I want output like.

Cancer_Disease=(Part1,Part2,Part3,Part4,Part5,Part6,Part7,Part8,Part9,Part10)

How to do this in R?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Aryh
  • 479
  • 1
  • 4
  • 16

3 Answers3

4

Note: I don't think the code you've listed will actually run, since you'd need quotation marks around the Part1 etc. terms (unless those are objects that have already been stored in memory somewhere else in your code).

That said: you can do this in base R with c() and unique():

Cancer_Disease <- unique(c(A.Cancer, B.Cancer, C.Cancer))

Putting the vectors in c() will string them together, and unique() will remove the repetitions.

Aaron Montgomery
  • 1,387
  • 8
  • 11
1

You can use union

a <- 1:10
b <- 5:15
c <- 12:20

Reduce(union, list(a, b, c))
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
1

Using reduce from purrr

library(purrr)
list(a, b, c) %>%
    reduce(union)

data

a <- 1:10
b <- 5:15
c <- 12:20
akrun
  • 874,273
  • 37
  • 540
  • 662