[R] List of Variables in Original Order
arun
smartpink111 at yahoo.com
Fri Sep 28 07:00:18 CEST 2012
HI,
May be this helps you:
set.seed(1)
mat1<-matrix(rnorm(60,5),nrow=5,ncol=12)
colnames(mat1)<-paste0("Var",1:12)
vec2<-format(c(1,cor(mat1[,1],mat1[,2:12])),digits=4)
vec3<-colnames(mat1)
arr2<-array(rbind(vec3,vec2),dim=c(2,3,4))
res<-data.frame(do.call(rbind,lapply(1:dim(arr2)[3],function(i) arr2[,,i])))
res
# X1 X2 X3
#1 Var1 Var2 Var3
#2 1.00000 0.27890 -0.61497
#3 Var4 Var5 Var6
#4 0.24916 -0.76155 0.30853
#5 Var7 Var8 Var9
#6 -0.46413 0.79287 0.05191
#7 Var10 Var11 Var12
#8 -0.06940 -0.53251 0.06766
A.K.
----- Original Message -----
From: rkulp <rkulp at charter.net>
To: r-help at r-project.org
Cc:
Sent: Thursday, September 27, 2012 6:26 PM
Subject: [R] List of Variables in Original Order
I am trying to Sweave the output of calculating correlations between one
variable and several others. I wanted to print a table where the
odd-numbered rows contain the variable names and the even-numbered rows
contain the correlations. So if VarA is correlated with all the variables in
mydata.df, then it would look like
var1 var2 var3
corr1 corr2 corr3
var4 var5 var6
corr4 corr5 corr6
.
.
etc.
I tried using a matrix for the correlations and another one for the variable
names. I built the correlation matrix using
x = matrix(format(cor(mydata.df[,1],mydata.df[,c(2:79)]),digits=4),nc=3)
and the variable names matrix using
y = matrix(ls(mydata.df[c(2:79)]),nc=3).
The problem is the function ls returns the names in alphabetical order,
columnar order.
How do I get the names in columnar order? Is there a better way to display
the correlation of a single variable with a large number of other variables?
If there is, how do I do it? I appreciate any help I can get. This is my
first project in R so I don't know much about it yet.
--
View this message in context: http://r.789695.n4.nabble.com/List-of-Variables-in-Original-Order-tp4644436.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list