R: Result vector is not showing up in For Loop

OK, so I am new to R, but I've had some pretty good success so far - I am running a statistical test between corresponding rows of two dataframes (well, one is just a string of values since it has just one column). I wish to use the following For-Loop:

zvalues = NULL
zvalues = numeric(0)
for(i in seq(nrow(geneexpx))){
  zvalues[i] <- try(unname((geneexpx[i]-rowMeans(geneexpy[i,])) / rowSds(geneexpy[i,])))
}

The problem is, the resultant zvalues numeric is empty. I have no idea why. I can run the same function for a single row and it works fine. For instance:

s = unname(geneexpx[4]-rowMeans(geneexpy[4,])) / rowSds(geneexpy[4,])
s
[1] -2.431277e+156

Please let me know if you have any ideas as to what might be the problem.

EDIT: head of geneexpx:

c(1.501400411, -0.818584726, -0.455614921, -0.138022494, -1.213938495, -0.536465133)  

geneexpy is very large, but each column is similar to geneexpx above.

Answers


You have a couple things going on here. First, you need to define zvalues as a vector. Second, rowMeans and rowSds are operations on matrices, not vectors. By selecting greneecxpy[i, ] you are selecting the ith row of the matrix, which will be a vector.

You did not provide geneexpy so I made one up:

zvalues = rep(NA, length(geneexpx))
geneexpy <- matrix(runif(60), nrow = 6)
for(i in seq_along(geneexpx)){
  zvalues[i] <- (geneexpx[i] - mean(geneexpy[i,])) / sd(geneexpy[i,])
}

> zvalues
[1]  3.772994 -4.283168 -2.812811 -2.074548 -5.649359 -4.323920

Need Your Help

Dynamic Images in Email spacing

html image email

I'm building an email in which there will be 3 images that load promo offers each month. Some months there might only be 2 offers and in place of the 3rd image the idea is to just save a 1x1 spacer...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.