I have two data frames. It is easy to calculate percent change from t1 to t2 like this:
t1 <- data.frame("gene1" = c(1,5,10), "gene2" = c(1,1,1), "gene3" = c(5,5,20))
row.names(t1) <- c("patient1", "patient2", "patient3")
t2 <- data.frame("gene1" = c(0.5,5,20), "gene2" = c(2,4,8), "gene3" = c(2.5,20,5))
row.names(t2) <- c("patient1", "patient2", "patient3")
t3 <- (t2-t1)/t1 *100
t3
#> gene1 gene2 gene3
#> patient1 -50 100 -50
#> patient2 0 300 300
#> patient3 100 700 -75
but what if I want to do symmetric percent change such that a value change from 20 to 5 would not be -75, but -300. I tried this:
t3 <- ifelse(t2 > t1, ((t2-t1)/t1) * 100, ((t2-t1)/t2) * 100)
but that gives me some weird list of 3x9.
In principle using ifelse should work. If I reduce the complexity then it works just fine
t3 <- ifelse(t2 > t1, "a", "b")
t3
#> gene1 gene2 gene3
#> patient1 b a b
#> patient2 b a a
#> patient3 a a b
Ideally my output would be:
t3
#> gene1 gene2 gene3
#> patient1 -100 100 -100
#> patient2 0 300 300
#> patient3 100 700 -300