0

I am a bit stumped on how to create a large matrix. The code I have so far are as given below. What I desire is that the rows are positions and columns are 'beta_values' from individual text files.

#Number of files = 300
#Matrix 33000 x 300
file.list <- list.files(pattern = "txt$")
# for each file, run read.table and select only the 1,2,3,12th column
columns = c('ID','S','E','Name','Alias','version','cdrive','positional','Contour','total_Contour','M_values','beta_values')

#Number of rows in the matrix
nr=33000

mat <- matrix("numeric", nrow = nr, ncol = length(file.list))

for (i in 1:length(file.list)) {
 fs <- fread(file.list[i], colClasses = columns, select=c(1,2,4,12))            
 # Creating Position values given by paste(fs$V1,'_', fs$V2,'_',fs$V4, sep="") and 'beta_values' given by fs$V12
 fs_reorder <- data.frame(paste(fs$V1,'_', fs$V2,'_',fs$V4, sep=""), fs$V12)
 mat[,i] <- as.matrix(fs_reorder)
}

Error:

Error in mat[, i] <- as.matrix(fs_reorder) : 
  number of items to replace is not a multiple of replacement length

Quick note:

> i=1
> fs <- fread(file.list[i], select=c(1,2,4,12)) 
> mat <- matrix(nrow = nr, ncol = length(file.list))  
> fs_reorder <- data.frame(paste(fs$V1,'_', fs$V2,'_',fs$V4, sep=""), fs$V12)
> mat[,i] <- as.matrix(fs_reorder)
Error in mat[, i] <- as.matrix(fs_reorder) : 
  number of items to replace is not a multiple of replacement length
> mat<- as.matrix(fs_reorder)

So this works for 1 file.

Looping through the files:

file.list_main <- list.files(pattern = "txt$")
file.list = file.list_main[1:2]
n   = length(position_mat)
k   = length(file.list)
mat <- matrix(nrow=n, ncol=length(file.list))
for (i in 1:length(file.list)) {
 fs <- fread(file.list[i], select=c(1,2,4,12))
 fs_reorder <- data.frame(paste(fs$V1,'_', fs$V2,'_',fs$V4, sep=""), fs$V12)
 positions = (paste(fs$V1,'_', fs$V2,'_',fs$V4, sep=""))
 betas = fs$V12    
 for(j in 1:k){
   for(i in 1:n){
        mat[i,j] = (positions[i]*betas[j])
   }
 }
}

Error:

Error in positions[i] * betas[j] : 
  non-numeric argument to binary operator

For reproducible analysis, please find the example below. Any help is very much appreciated.

set.seed(20430)
n   = 1000
k   = 3

fileA = rnorm(n)
fileB = rnorm(n)
fileC = rnorm(n)
positions = paste("loveletters_",rnorm(n),sep="")
betas <- cbind(fileA, fileB, fileC)
for(j in 1:k){
   for(i in 1:n){
        x[i,j] = (positions[i]*betas[j])
   }
}

Results:
Error in positions[i] * betas[j] : 
  non-numeric argument to binary operator

> length(positions)
[1] 1000
> ncol(betas)
[1] 3
> nrow(betas)
[1] 1000
user44552
  • 153
  • 1
  • 10
  • You've got some data type issues (probably? Hard to be sure without seeing anything reproducible). `matrix("numeric", ...)` will create a `character` matrix with the word `"numeric"` for every entry. The `columns` vector you have seems to be specifying the *names* of columns, but use use it in the `colClasses` argument of `fread`. Thne when you use `paste` you create a `character` class object which is coerced to `factor` by `data.frame()`... – Gregor Thomas May 12 '17 at 17:40
  • `fs_reorder` appears to be a 2-column data frame where the first column is a `factor` and the second is whatever class `fs$V12` is, but then you convert it to a (2-column) matrix and attempt to jam it into a single column of `mat`. – Gregor Thomas May 12 '17 at 17:41
  • Moreover, it's not clear if *any* of your for loop iterations work or if the problem is just with one. (A `print(i)` line in your for loop can help you track it's progress to find out.) There are so many potential bugs that my guess is none of it is working. Get rid of the `for` loop and check your code line-by-line with `i <- 1`. When that works, then you can try to iterate. – Gregor Thomas May 12 '17 at 17:43
  • If you need additional help, you'll need to [create a reproducible example](http://stackoverflow.com/q/5963269/903061), preferably something **minimal** with, say, a 5-line test file not a (presumably) 33k line test file. – Gregor Thomas May 12 '17 at 17:44
  • So I took your advice, the changes I have listed is above - Quick note - – user44552 May 12 '17 at 17:46
  • I analysed via each file but still unable to do a large matrix. I have submitted a reproducible example above. Many thanks for your help. – user44552 May 12 '17 at 18:35

0 Answers0