r - The condition has length > 1 and only the first element will be used -
i have dataframe, trip:
> head(trip.mutations) ref.y variant.y 1 t c 2 g c 3 c 4 t c 5 c 6 g
i want add third column, muttype, 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 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 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 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 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 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 c:g" } }
but error:
warning messages: 1: in if (trip$ref.y == "g" & trip$variant.y == "t" | trip$ref.y == ... : condition has length > 1 , first element used
i don't think logical statements should producing vectors, maybe i'm missing something. trip$muttype should end looking this:
muttype a:t g:c g:c c:g a:t c:g a:t g:c g:c t:a g:c a:t
can spot what's wrong here? need || instead of | perhaps?
you error because if
can evaluate logical
vector of length 1.
maybe miss difference between &
(|
) , &&
(||
). shorter version works element-wise , longer version uses 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 if
statement @ all:
mut1 <- trip$ref.y=='g' & trip$variant.y=='t'|trip$ref.y=='c' & trip$variant.y=='a' trip[mut1, "muttype"] <- "g:c t:a"
Comments
Post a Comment