myX.
myX.

Reputation: 115

R- how to access elements of a more 'complex' list, plus transform my complex list into a dataframe

I've got a little bit frustrated because I couldn’t figure out how to work properly with a more 'complex' list in R. Please, have a look.

I'm starting with this list here:

> test.results
  [,1]      [,2]      [,3]      [,4]      [,5]     
A 0.9375027 0.9995755 0.9997706 0.6900084 0.9207406
B 0.9375027 0.9375027 0.9997706 0.9375027 0.9207406
C 0.9375027 0.9375027 0.9997706 0.9375027 0.9954647

I would like to apply a function on each block (block#1 would be [,1]) of the list.

> as.matrix(test.results[,1])
  [,1]     
A 0.9375027
B 0.9375027
C 0.9375027

Let's say I want to compute the mean separately for each block [,1] to [,5] over A,B,C. For block#1, this would be the same like computing the mean for the vector c(0.9375027, 0.9375027, 0.9375027), for block#2 the mean for vector c(0.9995755, 0.9995755, 0.9995755), ...

Optionally, I'm also interested in transforming the list into a dataframe:

1 A 0.9997706
1 B 0.9997706
1 C 0.9997706
2 A 0.9995755
2 B 0.9375027
2 C 0.9375027
. . . .
5 A  0.9207406 
5 B  0.9207406
5 C  0.9954647

1,2,..,5 indicate the original block number from the list here.

Thanks in advance.

Upvotes: 0

Views: 152

Answers (2)

Michele
Michele

Reputation: 8753

Try this:

colMeans(test.results)

Then for changing your matrix you can use melt from reshape2

> m <- matrix(1:12, ncol=4)
> library(reshape2)
> melt(m)
   Var1 Var2 value
1     1    1     1
2     2    1     2
3     3    1     3
4     1    2     4
5     2    2     5
6     3    2     6
7     1    3     7
8     2    3     8
9     3    3     9
10    1    4    10
11    2    4    11
12    3    4    12

(In your case Var1 and Var2 would be 1,2,..,5 and A,B, and C)

EDIT:

test.result <- as.matrix(read.delim(text="0.9375027 0.9995755 0.9997706 0.6900084 0.9207406
0.9375027 0.9375027 0.9997706 0.9375027 0.9207406
0.9375027 0.9375027 0.9997706 0.9375027 0.9954647", header=F, sep=" "))

rownames(test.result) <- LETTERS[1:3]
colnames(test.result) <- NULL

> test.result
       [,1]      [,2]      [,3]      [,4]      [,5]
A 0.9375027 0.9995755 0.9997706 0.6900084 0.9207406
B 0.9375027 0.9375027 0.9997706 0.9375027 0.9207406
C 0.9375027 0.9375027 0.9997706 0.9375027 0.9954647

> class(test.result)
[1] "matrix"

> colMeans(test.result)
[1] 0.9375027 0.9581936 0.9997706 0.8550046 0.9456486

EDIT 2:

You have a list "structured" as a matrix... like this probably:

test <- structure(list(1, 11, 111, 2, 22, 222), .Dim = c(3L, 2L), .Dimnames = list(
  NULL, c("a", "b")))

> test
     a   b  
[1,] 1   2  
[2,] 11  22 
[3,] 111 222

> is.list(test)
[1] TRUE
> is.matrix(test)
[1] TRUE
> colMeans(test)
Error in colMeans(test) : 'x' must be numeric

If so, you just need to unlist and then give the object the previous structure:

> (test <- matrix(unlist(test), nrow(test)))
     [,1] [,2]
[1,]    1    2
[2,]   11   22
[3,]  111  222
> is.list(test)
[1] FALSE
> colMeans(test)
[1] 41 82

Upvotes: 1

agstudy
agstudy

Reputation: 121568

For your the columns mean compute , colMeans is the best answer.

To put your matrix in the long format ( the second part of your question) you can use stack:

    dd <- stack(test.results)
    dd$ind <- rownames(test.results)

      values ind
1  0.9375027   A
2  0.9375027   B
3  0.9375027   C
4  0.9995755   A
5  0.9375027   B
6  0.9375027   C
7  0.9997706   A
8  0.9997706   B
9  0.9997706   C
10 0.6900084   A
11 0.9375027   B
12 0.9375027   C
13 0.9207406   A
14 0.9207406   B
15 0.9954647   C

Upvotes: 1

Related Questions