Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

r - The condition has length > 1 and only the first element will be used

I have a dataframe, trip:

> head(trip.mutations)
  Ref.y Variant.y
1 T     C 
2 G     C 
3 A     C  
4 T     C 
5 C     A 
6 G     A 

I want to add a third column, mutType, that follows these rules:

for (i in 1:nrow(trip)) {
   if(trip$Ref.y=='G' & trip$Variant.y=='T'|trip$Ref.y=='C' & trip$Variant.y=='A') {
      trip[i, 'mutType'] <- "G:C to T:A"
   }
   else if(trip$Ref.y=='G' & trip$Variant.y=='C'|trip$Ref.y=='C' & trip$Variant.y=='G') {
      trip[i, 'mutType'] <- "G:C to C:G"
   }
   else if(trip$Ref.y=='G' & trip$Variant.y=='A'|trip$Ref.y=='C' & trip$Variant.y=='T') {
      trip[i, 'mutType'] <- "G:C to A:T"
   }
   else if(trip$Ref.y=='A' & trip$Variant.y=='T'|trip$Ref.y=='T' & trip$Variant.y=='A') {
      trip[i, 'mutType'] <- "A:T to T:A"
   }
   else if(trip$Ref.y=='A' & trip$Variant.y=='G'|trip$Ref.y=='T' & trip$Variant.y=='C') {
      trip[i, 'mutType'] <- "A:T to G:C"
   }
   else if(trip$Ref.y=='A' & trip$Variant.y=='C'|trip$Ref.y=='T' & trip$Variant.y=='G') {
      trip[i, 'mutType'] <- "A:T to C:G"
   }
}

but I get the error:

Warning messages:
1: In if (trip$Ref.y == "G" & trip$Variant.y == "T" | trip$Ref.y ==  ... :
  the condition has length > 1 and only the first element will be used

I don't think my logical statements should be producing vectors, but maybe I'm missing something. trip$mutType should end up looking like this:

mutType
A:T to G:C
G:C to C:G
A:T to C:G
A:T to G:C
G:C to T:A
G:C to A:T

Can anyone spot what's wrong here? Do I need || instead of | perhaps?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You get the error because if can only evaluate a logical vector of length 1.

Maybe you miss the difference between & (|) and && (||). The shorter version works element-wise and the longer version uses only the first element of each vector, e.g.:

c(TRUE, TRUE) & c(TRUE, FALSE)
# [1] TRUE FALSE

# c(TRUE, TRUE) && c(TRUE, FALSE)
[1] TRUE

You don't need the if statement at all:

mut1 <- trip$Ref.y=='G' & trip$Variant.y=='T'|trip$Ref.y=='C' & trip$Variant.y=='A'
trip[mut1, "mutType"] <- "G:C to T:A"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...