How to calculate ratios and normalized ratios in all possible combinations in R?

陌路散爱 提交于 2020-08-10 19:18:39

问题


I want to calculate normalised ratios and simple ratios in all possible combinations in R. This is the sample dataset

df = structure(list(var_1 = c(0.035, 0.047, 0.004, 0.011, 0.01, 0.01, 
0.024), var_2 = c(0.034, 0.047, 0.004, 0.012, 0.01, 0.011, 0.025
), var_3 = c(0.034, 0.047, 0.006, 0.013, 0.011, 0.013, 0.026), 
    var_4 = c(0.034, 0.046, 0.008, 0.016, 0.014, 0.015, 0.028
    ), var_5 = c(0.034, 0.046, 0.009, 0.017, 0.015, 0.016, 0.029
    )), class = "data.frame", row.names = c(NA, -7L))

I could able to calculate simple ratios in all possible combinations after taking help from this.

do.call("cbind", lapply(seq_along(df), function(y) apply(df, 2, function(x) df[[y]]/x)))

But I am unable to calculate normalised ratios i.e. (xj - xi)/(xj + xi) and how to name each calculated ratios properly?


回答1:


Perhaps, you can try nested lapply to get all the combinations :

cols <- 1:ncol(df)
mat <- do.call(cbind, lapply(cols, function(xj) 
          sapply(cols, function(xi) (df[, xj] - df[, xi])/(df[, xj] + df[, xi]))))

To assign column names, we can use outer

colnames(mat) <-  outer(names(df), names(df), paste0)

Thinking about it I think we can directly manipulate this using column indexes.

cols <- 1:ncol(df)
temp <- expand.grid(cols, cols)
new_data <- (df[,temp[,2]] - df[,temp[,1]])/(df[,temp[,2]] + df[,temp[,1]])



回答2:


We could do this more easily with outer alone

f1 <- function(i, j) (df[, i] - df[, j])/(df[, i] + df[, j])
out <- outer(seq_along(df), seq_along(df), FUN = f1)
colnames(out) <- outer(names(df), names(df), paste0)


来源:https://stackoverflow.com/questions/62915303/how-to-calculate-ratios-and-normalized-ratios-in-all-possible-combinations-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!