Warunek ma długość> 1 i będzie użyty tylko pierwszy element
Mam 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
Chcę dodać trzecią kolumnę, mutType, która podąża za tymi zasadami:
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"
}
}
Ale dostaję błąd:
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
Nie wydaje mi się, żeby moje logiczne stwierdzenia tworzyły wektory, ale może coś mi umyka. trip$mutType powinien skończyć tak:
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
Czy ktoś może zauważyć, co tu jest nie tak? Czy potrzebuję | / zamiast / być może? 23
2 answers
Otrzymujesz błąd, ponieważ {[2] } możesz ocenić tylko logical
wektor długości 1.
Może brakuje Ci różnicy między &
(|
) oraz &&
(||
). Krótsza wersja działa elementowo, a dłuższa używa tylko pierwszego elementu każdego wektora, np.:
c(TRUE, TRUE) & c(TRUE, FALSE)
# [1] TRUE FALSE
# c(TRUE, TRUE) && c(TRUE, FALSE)
[1] TRUE
W ogóle nie potrzebujesz if
wypowiedzi:
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"
38
Author: sgibb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-26 20:41:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-26 20:41:02
Jak sgibb powiedział, że to problem if, nie miał nic wspólnego z |lub|/.
Oto inny sposób na rozwiązanie twojego problemu:
for (i in 1:nrow(trip)) {
if(trip$Ref.y[i]=='G' & trip$Variant.y[i]=='T'|trip$Ref.y[i]=='C' & trip$Variant.y[i]=='A') {
trip[i, 'mutType'] <- "G:C to T:A"
}
else if(trip$Ref.y[i]=='G' & trip$Variant.y[i]=='C'|trip$Ref.y[i]=='C' & trip$Variant.y[i]=='G') {
trip[i, 'mutType'] <- "G:C to C:G"
}
else if(trip$Ref.y[i]=='G' & trip$Variant.y[i]=='A'|trip$Ref.y[i]=='C' & trip$Variant.y[i]=='T') {
trip[i, 'mutType'] <- "G:C to A:T"
}
else if(trip$Ref.y[i]=='A' & trip$Variant.y[i]=='T'|trip$Ref.y[i]=='T' & trip$Variant.y[i]=='A') {
trip[i, 'mutType'] <- "A:T to T:A"
}
else if(trip$Ref.y[i]=='A' & trip$Variant.y[i]=='G'|trip$Ref.y[i]=='T' & trip$Variant.y[i]=='C') {
trip[i, 'mutType'] <- "A:T to G:C"
}
else if(trip$Ref.y[i]=='A' & trip$Variant.y[i]=='C'|trip$Ref.y[i]=='T' & trip$Variant.y[i]=='G') {
trip[i, 'mutType'] <- "A:T to C:G"
}
}
2
Author: Jingtai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-28 21:16:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-28 21:16:43