Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign values to row of a matrix when matrix name is a character string

Tags:

r

I want to create several matrixes and assign values which are obtained through the function to the specific rows automatically. I'll give example of what I've tried so far:

EXAMPLE1<-matrix(ncol=4, nrow=16)
EXAMPLE2<-matrix(ncol=4, nrow=16)
EXAMPLE3<-matrix(ncol=4, nrow=16)
EXAMPLE4<-matrix(ncol=4, nrow=16)


for(i in 1:16){
  for(j in 1:4){
    paste0("EXAMPLE",j, "[",i,",","]")<- c(1:4)
  }
}

and

for(i in 1:16){
  for(j in 1:4){
    get(paste0("EXAMPLE",j)[i,]<- c(1:4)
  }
}

but "target of assignment expands to non-language object" is returned. So I tried

for(i in 1:16){
  for(j in 1:4){
    assign(paste0("EXAMPLE",j, "[",i,",","]"), c(1:4))
  }
}

This does not return error, but values are not written into matrix.

At first matrixes are empty

     [,1] [,2] [,3] [,4]
[1,]   NA   NA   NA   NA
[2,]   NA   NA   NA   NA
[3,]   NA   NA   NA   NA
[4,]   NA   NA   NA   NA

and after one loop is performed (i=1) I expext all EXAMPLE matrixes to have 1 row filled (and after second - second row filled, so that when loop ends I would have EXAMPLE matrix full of numbers.).

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]   NA   NA   NA   NA
[3,]   NA   NA   NA   NA
[4,]   NA   NA   NA   NA

The point is though, i do not how to assign value to the matrix row, because I pass matrix name by paste function. How to assign values to a specific row in a matrix if I want to put its name as character string? Any help would be wonderful. Thanks

...Edition... I am sorry that I am not very clear. As I mentioned, I want to write result of a function (which happens to have same number of columns as my matrix) into matrix row. Each row would take result from function applied to different observation. I want to apply several functions to same variable so I have several matrixes for results of the different function (which differ in column numbers and have different names). The way I numbered and named matrixes is the easiest to keep track of results and later compare them. Above I gave just an example of problem which is that I cannot assign value to a row of matrix because I give name of matrix as string (and I do that because otherwise there would be quite a lot of numbering to do). So in general I want to be able to do

EXAMPLEj[i,]<-SomeFunctionResult

Where j goes 1 to 10 or some other numbers

Thats why I use loop and paste0("EXAMPLE", j) to generate names of matrixes and i to denote which observation for function to use and in which row to put the result. Matrixes are already created, but I found no way to pass results to row of the matrix.

I am new at R (and programming) so it is possible I've chosen the worst way possible to solve this and that list or arrays would be much easier to use. Thank you for your responses.

like image 771
user2287846 Avatar asked Nov 02 '22 23:11

user2287846


1 Answers

Put the individual matrices into a list:

EXAMPLE <- list(EXAMPLE1, EXAMPLE2, EXAMPLE3, EXAMPLE4)

Then, simply access the matrices in this list by index:

for(i in 1:16){
  for(j in 1:4){
    EXAMPLE[[j]][i,] <- c(1:4)
  }
}

The list can also be named, and accessed by the names:

EXAMPLE <- list(EXAMPLE1=EXAMPLE1, EXAMPLE2=EXAMPLE2,
                EXAMPLE3=EXAMPLE3, EXAMPLE4=EXAMPLE4)
EXAMPLE[['EXAMPLE1']]
like image 121
krlmlr Avatar answered Nov 14 '22 04:11

krlmlr