Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if and & statement in R

Tags:

r

I have a data.frame with 4 columns like that:

> bb
               V1      V2      V3       V4
1         ARFGEF2 ARFGEF2 ARFGEF2     <NA>
2           SFRS5    <NA>   SRSF5     <NA>
3 ENSG00000215104    <NA>    <NA> CHMP1B2P
4            EDF1    EDF1    EDF1     <NA>
5    LOC100133678    <NA>    <NA>     <NA>
6            CD3G    CD3G       -     <NA>
7           GNAI2   GNAI2   GNAI2     <NA>

I want to create a new column according to values on columns 2, 3, and 4. What I was trying to do is : If bb[,2] is a NA AND bb[,3] has a value then bb[,5] would be the value of bb[,3], if bb[,2] is a NA AND bb[,3] is a NA and df[,4] has a value then bb[,5] would be the value of bb[,4], else bb[,5] would be df[,1]. Here is the expected output:

> bb
                V1      V2      V3       V4       V5
1          ARFGEF2 ARFGEF2 ARFGEF2     <NA>  ARFGEF2
2            SFRS5    <NA>   SRSF5     <NA>    SRSF5
3  ENSG00000215104    <NA>    <NA> CHMP1B2P CHMP1B2P
4             EDF1    EDF1    EDF1     <NA>     EDF1
5     LOC100133678    <NA>    <NA>     <NA>     <NA>
6             CD3G    CD3G       -     <NA>        -
7            GNAI2   GNAI2   GNAI2     <NA>    GNAI2

I've trying this code but it doesn't work:

> for (i in 1:nrow(bb)){
      if (is.na(bb[i,2] & !(is.na(bb[i,3])))) {bb[i,5] <- as.character(bb[i,3])}
    else if (is.na(bb[i,2]) & !(is.na(bb[i,4]))) {bb[i,5] <- as.character(bb[i,4])}
    else { bb[i,5] <- bb[i,1]}
}
Warning messages:
 1: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 2: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 3: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 4: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 5: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 6: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors
 7: In Ops.factor(bb[i, 2], !(is.na(bb[i, 3]))) :
  & not meaningful for factors

How could I use & with the if statement? This is an example of a much longer data.frame where I would like to create a new column (V5) taking the values of V1 if V2 is not a NA. In case of V2 being a NA, first I want to check whether V3 has a value and if it is TRUE then V5 would be V3 value, second, if V3 is a NA and V4 has a value then V5 would be the value of V4, finally if V3 and V4 are NA then V5 would be V1.

Many thanks

like image 210
user2380782 Avatar asked Oct 02 '22 02:10

user2380782


1 Answers

Your for loop should be using && instead of &. Also a missing bracket at the first is.na. But try the ifelse function, something along the lines

bb[,5] <- ifelse(is.na(bb[,2]) & !is.na(bb[,3]), 
            bb[,3], 
            ifelse(is.na(bb[,2]) & !is.na(bb[,4]), bb[,4], bb[,1])
          )

(untested)

like image 137
Karsten W. Avatar answered Oct 03 '22 16:10

Karsten W.