Change or modify function from ratio to subtraction in R

Hi,

I need a help to modify the calculations of part of an R code. One of my collaborator has provided me with a R script to perform certain operations for transcriptomics analysis in R. The code is provided below. It seems like the current code calculates the ratio of linear values of two groups to output the fold changes of the genes, however, I would like to calculate the difference of two groups instead of the ratio since my data is in log2 scale. Please assist me in modifying the code from the ratio to the difference mode.

k=1
for (k in 1:nrow(df_raw)) {
  signature = rownames(df_raw)[k]
  test.table <- sample_info
  test.table$scores <- df_raw[k,]
  for (i in 1:length(group.test)) {
    group = group.test[i]
    T2 <- test.table[test.table$Disease==group,]
    T1 <- test.table[test.table$Disease==c("Healthy Control"),]
    FC.group[signature,group] <- foldchange(mean(T2$scores),mean(T1$scores))
  }
}

FCgroup <- data.frame(FC.group)

dput(df_raw)
structure(list(GSM627008 = c(5.65559787660601, 4.62874325571269, 
4.85599479635638, 5.85254244806934, 5.80777520984508, 5.80777520984508, 
4.98047234412286, 4.91464408539888, 4.91464408539888, 5.18779519031756
), GSM627009 = c(5.66193349478876, 5.25071017072815, 5.28529495832709, 
6.05060511151731, 5.91473681593762, 5.91473681593762, 5.04605000681339, 
4.84369038755599, 4.84369038755599, 4.79521271270339), GSM627010 = c(6.31192956034828, 
5.36637667484634, 5.13314768312592, 5.23622994977253, 5.54928243194798, 
5.54928243194798, 5.43214187397047, 4.97987441704922, 4.97987441704922, 
5.17434505859315), GSM627011 = c(5.83741241440251, 5.5634795897132, 
5.28114307188208, 5.25130798399716, 5.37556990566231, 5.37556990566231, 
5.09637172753254, 4.98487196255991, 4.98487196255991, 4.85484427106761
), GSM627012 = c(5.69287367577666, 5.29433723216866, 5.01762428312124, 
5.83250286550024, 6.07807566487418, 6.07807566487418, 5.24105456966276, 
5.0900373324603, 5.0900373324603, 4.88349310614953), GSM1707490 = c(5.94512799980027, 
5.18710416214502, 4.17023996028716, 6.78944822514053, 6.00049751333671, 
6.00049751333671, 4.60784065443623, 4.70831021477247, 4.70831021477247, 
4.09861980564098), GSM1707491 = c(6.38487112284085, 4.84455095522761, 
4.36085352075376, 7.24704741580461, 6.00697822839186, 6.00697822839186, 
5.17214334019319, 4.97363585366777, 4.97363585366777, 4.22852286268715
), GSM1707492 = c(6.1778292980783, 4.36138769422303, 4.35547195294666, 
6.14980769425548, 6.05078732097762, 6.05078732097762, 4.84081734855588, 
4.7419421345705, 4.7419421345705, 4.40904517600504), GSM1707493 = c(6.43851448574189, 
4.93686212863045, 5.08669738892517, 6.10557734572431, 5.40861475442721, 
5.40861475442721, 4.76419821298975, 4.92390894566459, 4.92390894566459, 
5.00521207610614), GSM1707494 = c(5.0896260895405, 5.82483381014864, 
5.51303933194922, 5.1962043727938, 5.07349819659628, 5.07349819659628, 
6.01815175633989, 5.16503571340461, 5.16503571340461, 5.20212654555871
), GSM1707495 = c(5.54930205258425, 5.54533332156524, 4.80169034349181, 
5.42897486577672, 5.01552353268956, 5.01552353268956, 5.19122603971155, 
5.18714819719997, 5.18714819719997, 4.86343643937811)), class = "data.frame", row.names = c("Gene1", 
"Gene2", "Gene3", "Gene4", "Gene5", 
"Gene6", "Gene7", "Gene8", "Gene9", 
"Gene10"))
dput(sample_info)
structure(list(Dataset = c("GSE25504", "GSE25504", "GSE25504", 
"GSE25504", "GSE25504", "GSE69686", "GSE69686", "GSE69686", "GSE69686", 
"GSE69686", "GSE69686"), Disease = c("Healthy Control", "Healthy Control", 
"Healthy Control", "Healthy Control", "Healthy Control", "Sepsis", 
"Sepsis", "Sepsis", "Sepsis", "Sepsis", "Sepsis")), row.names = c("GSM627008", 
"GSM627009", "GSM627010", "GSM627011", "GSM627012", "GSM1707490", 
"GSM1707491", "GSM1707492", "GSM1707493", "GSM1707494", "GSM1707495"
), class = "data.frame")
group.test 
"Healthy Control" "Sepsis"

Thank you,
Toufiq

Hello,

The data and code are a bit messy, so I'm not sure what you want exactly, but if all you want is to find the difference between the groups instead of the foldchange, you can change this line:

FC.group[signature,group] <- foldchange(mean(T2$scores),mean(T1$scores))

into this

FC.group[signature,group] <- mean(T2$scores) - mean(T1$scores)

If you like the absolute difference, just wrap the subtraction in abs()

Hope this helps,
PJ

1 Like

@pieterjanvc, thank you very much for the response.

Yes, I want to find the difference between the two group since my data is in log2 scale instead of ratio. I will try your solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.