Search code examples
rbioinformaticsspss

Compute multiple arrays of same variables into one Variables in R?


How to compute different parameters as one in R. For example. I have 3 arrays of a variable A called A1.1,A1.2,A1.3. I want to compute them in one as "A". How to do that?

A1.1>c(1,1,1,0,0,0)
A1.2>c(1,0,0,1,1,1)
A1.3>c(0,1,1,1,1,1)

Out put should be like this. in SPSS we do this by compute variables.

A>c(1,1,1,1,1,1)

Solution

  • In R you can use simple math on arrays, for example:

    A1.1 <- c(1,0,1,0,0,0)
    A1.2 <- c(1,0,0,1,1,1)
    A1.3 <- c(0,0,1,1,1,1)
    A1 <-  1*((A1.1 + A1.2 + A1.3)>0)
    
    > A1
    [1] 1 0 1 1 1 1