How to save or reserve a list of objects? (R)

2

In R, I want to "save a list of arrays".

Actually, I'm wanting to reserve a list of 10 objects, where each object will turn an array, after using a for ()

Something like this:

Data <- uma base de dados

Lista <- é a lista que eu quero guardar ou reservar (criar?)

for(i in 1:10)
{

 Lista[i]<- subset(Data, coluna1 == levels(Coluna1)[i] )

}

In this way, List is a list of 10 arrays ...

    
asked by anonymous 27.08.2014 / 21:37

2 answers

2

Well, you can either store arrays in a list or in an array.

With the array the idea would be to use a three-dimensional array. The third dimension would be an index of the matrices. However, for this solution, all matrices must be equal in size.

For example, the following command generates an array that can be interpreted as the following: it stores 10 2 x 2 arrays.

matrizes <- array(dim= c(2,2,10))

Then you can populate this array by indexing the third dimension:

set.seed(1)
for (i in 1:10){
  matrizes[,,i] <- matrix(rnorm(4), ncol=2)
}

So, to access the first array:

matrizes[,,1]
           [,1]       [,2]
[1,] -0.6264538 -0.8356286
[2,]  0.1836433  1.5952808

or the tenth array

matrizes[,,10]

           [,1]      [,2]
[1,] -0.3942900 1.1000254
[2,] -0.0593134 0.7631757

The other option is as you yourself said, store in a list. Lists are very flexible in R, so if you want to store other objects together with the arrays, or if the arrays are of different sizes, the list is more appropriate.

matrizes <- list()

set.seed(1)
for (i in 1:10){
  matrizes[[i]] <- matrix(rnorm(4), ncol=2)
}

Accessing array 1:

matrizes[[1]]
          [,1]       [,2]
[1,] -0.6264538 -0.8356286
[2,]  0.1836433  1.5952808

And the array 10:

matrizes[[10]]
           [,1]      [,2]
[1,] -0.3942900 1.1000254
[2,] -0.0593134 0.7631757
    
28.08.2014 / 00:14
0

Vasco if I'm not wrong you have a typical task in which the most elegant solution is a split. The solution I'm going to introduce you to is when you want to separate a dataset from one-factor levels. For example I'll use the iris dataset:

## Carregando os dados
data(iris)

## Vendo as primeiras linhas
head(iris)

See that the last column is the species. Let's see how many species there are

## Mostrando os níveis de Species
levels(iris$Species)

See that there are three: [1] "setosa" "versicolor" "virginica"

Now I will separate the data.frame into three, creating a list, where each element of the list is a data.frame containing only the lines relative to the factor level specified in the split.

## Separando por espécie
lista <- split(x = iris, f = iris$Species)
Ready! Now I can apply any method in the separate parts. Suppose I want to calculate the average of the Sepal.Length variable for each species:

## Média por espécie
lapply(lista, function(x) mean(x$Sepal.Length))

$setosa
[1] 5.006

$versicolor
[1] 5.936

$virginica
[1] 6.588

If it is a simple function like the average, considering only one factor, there is a much faster way with tapply:

## Média por espécie
tapply(iris$Sepal.Length, iris$Species, mean)

     setosa versicolor  virginica 
     5.006      5.936      6.588 

But note that the solution with split is more general and applies to more complex situations involving operations with more than one column.

    
18.09.2014 / 07:21