Pergunta

I have a list of matrices, all of equal dimensions. Each matrix within the list represents a different specimen; each matrix contains three columns for X, Y and Z coordinates, and each row represents a different point in 3D space (i.e., an identifiable landmark).

Most specimens are missing coordinate data for particular landmarks (so that all three columns contain NAs). I would like to subset all matrices in the list so that they only include landmarks/rows containing complete data (i.e., no NAs exist in that row for any of the specimens/matrices in the entire list).

I fear this may be quite a complicated task for data stored in list format. As all the matrices have the same dimensions, would it be easier to convert the data to an array? I wanted to avoid doing this as it would (I believe) strip the row, column and list-element names I use to identify the data.

Foi útil?

Solução

For example, using complete.cases:

res <- lapply(your_list,function(mat)
                   mat[complete.cases(mat),]

An if your matrices, have the same number of columns, you can put the result in a big matrix using something like:

do.call(rbind,res)

Outras dicas

The best thing to do is first use

do.call(rbind,res)

then with a single matrix containing all list sub matrices add one more column And one more column to label the rows of each sub matrix. So if your your sub matrices has 3 rows each, the this column will look like: 1,2,3,1,2,3,...,1,2,3 e.g

    singleMatrix=do.call(rbind,res)

rowindex=rep(c(1:numberOfRowsOfSubMatrix,numberOfSubMatrices) Then form a combined data frame with the indicator, singMatrix and rowindex

Matrix=data.frame(singleMatrix,indicator,rowindex)

Now if indicator==0 delete the row and delete all rows with thesame rowindex number.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top