Lamda
Lamda

Reputation: 944

Save variables in a matrix in R

Is it possible to store variables in R..

I have to compute 10*324 fitdist's which each output from this function has to be stored inside a matrix with the size above? is that possible in R?

If so then how, i am completely lost?

So i tried creating a simple example

norm_dist <- as.data.frame(matrix(nrow=3,ncol=3))
data(iris)
for(i in 1:3)
{
  for(j in 1:3)
  {
    print(i)
    print(j)
    if(j==1)
    {
      element = fitdist(data =iris$Petal.Width[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
    if(j==2)
    {
      element = fitdist(data =iris$Petal.Length[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
   if(j==3)
    {
      element = fitdist(data =iris$Sepal.Length[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
  }

}

But i am getting this error

Error in `[<-.data.frame`(`*tmp*`, i, j, value = list(estimate = c(0.867771222640304,  : 
  replacement element 4 is a  matrix with 2 rows, needs 1 

I am not sure i understand what it means...

Upvotes: 0

Views: 1391

Answers (1)

chinsoon12
chinsoon12

Reputation: 25225

you might want to check out the ?fitdist documentation under "Value" section. It mentions the output from the function which is a list with a few components.

Which of those value do you want to assign into norm_dist? For example, if you want the log-likelihood, you can use norm_dist[i,j] = element$loglik

If you want to store the whole object to be stored, you will need a list rather than a data.frame, for e.g.

norm_dist_res <- list()
for(i in 1:10)
{
    for(j in 1:324)
    {
        norm_dist_res[[paste0(i,"-",j)]] <- fitdist(data=g_all_p$data[1:8000*i, j], distr="norm")
    }
}

Upvotes: 1

Related Questions