开发者

Redefining and merging two tables in R or SAS

I have two tables (matrix) of same dimensions, one contains correlation coefficients and other with p values. I want to combine them into one table. For example let's say I have correlation coefficient between variable A1 and A2 of 0.75 in table 1 and p value of 0.045 in table 2. Now in my combined table 3, I want to use:

condition1 for table 1: if a coefficient value in a cell of table 1 is less than 0.4 then "+", 0.4 <= coefficient <0.7 then "++" else "+++",

condition2 for table 2: if a pvalue in a cell of table开发者_运维问答 2 is less than 0.01 then "+++", 0.01 <= pvalue < .05 then "++" else "+".

Thus corresponding cell value for A1 and A2 in table 3 should look like: +++/++ where "+++" correspond to table 1 value of 0.75 and ++ correspond to table 2 p value of 0.045 and "/" is just a separator.

I would like to do this either is SAS or R.


Here is a solution with R

First, create some dummy data to work with

corr <- matrix(runif(16),4,4)
ps <- matrix(runif(16)^5,4,4)

Each matrix can be formatted separately. Note that this drops them down to vectors. The matrix structure will be restored after pasting together the two formatted versions.

corr.fmt <- cut(corr, c(0, 0.4, 0.7, 1), labels=c("+","++","+++"))
ps.fmt <- cut(ps, c(0, 0.01, 0.05, 1), labels=c("+++","++","+"))
res <- matrix(paste(corr.fmt, ps.fmt, sep="/"), nrow(corr), ncol(corr))

This could be combined into a single statement if you want to put the transformations inline

res <- matrix(paste(cut(corr, c(0, 0.4, 0.7, 1), labels=c("+","++","+++")), 
                    cut(ps, c(0, 0.01, 0.05, 1), labels=c("+++","++","+")), 
                    sep="/"), nrow(corr), ncol(corr))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜