简体   繁体   中英

Why is t() returning a 'vector'?

Just trying to some basic matrix algebra in R and I'm getting some weird results that I don't completely understand.

So, my data looks like this:

    Wt LvrWt Dose Y
1   176 6.5 0.88    0.42
2   176 9.5 0.88    0.25
3   190 9.0 1.00    0.56
4   176 8.9 0.88    0.23
5   200 7.2 1.00    0.23
6   167 8.9 0.83    0.32
7   188 8.0 0.94    0.37
8   195 10.0 0.98   0.41
9   176 8.0 0.88    0.33
10  165 7.9 0.84    0.38
11  158 6.9 0.80    0.27
12  148 7.3 0.74    0.36
13  149 5.2 0.75    0.21
14  163 8.4 0.81    0.28
15  170 7.2 0.85    0.34
16  186 6.8 0.94    0.28
17  146 7.3 0.73    0.30
18  181 9.0 0.90    0.37
19  149 6.4 0.75    0.46

And here is the code I'm using:

# Creating the X matrix
Xmatrix <- subset(questionOneA, select = -c(Y))
Xmatrix <- matrix(Xmatrix)
Xmatrix <- sapply(Xmatrix, as.numeric)
is.numeric(Xmatrix)

# Transposing the x matrix
Xtranspose <- t(Xmatrix)
Xtranspose <- matrix(Xtranspose)
is.numeric(Xtranspose)

The output of Xmatrix seems correct:

V1 V2 V3
1   176 6.5 0.88
2   176 9.5 0.88
3   190 9.0 1.00
4   176 8.9 0.88
5   200 7.2 1.00
6   167 8.9 0.83
7   188 8.0 0.94
8   195 10.0 0.98
9   176 8.0 0.88
10  165 7.9 0.84
11  158 6.9 0.80
12  148 7.3 0.74
13  149 5.2 0.75
14  163 8.4 0.81
15  170 7.2 0.85
16  186 6.8 0.94
17  146 7.3 0.73
18  181 9.0 0.90
19  149 6.4 0.75

However, the output of Xtranspose seems strange to me:

    
V1
1   176.00
2   6.50
3   0.88
4   176.00
5   9.50
6   0.88
7   190.00
8   9.00
9   1.00
10  176.00
11  8.90
12  0.88
13  200.00
14  7.20
15  1.00
16  167.00
17  8.90
18  0.83
19  188.00
20  8.00
21  0.94
22  195.00
23  10.00
24  0.98
25  176.00
26  8.00
27  0.88
28  165.00
29  7.90
30  0.84
31  158.00
32  6.90
33  0.80
34  148.00
35  7.30
36  0.74
37  149.00
38  5.20
39  0.75
40  163.00
41  8.40
42  0.81
43  170.00
44  7.20
45  0.85
46  186.00
47  6.80
48  0.94
49  146.00
50  7.30
51  0.73
52  181.00
53  9.00
54  0.90
55  149.00
56  6.40
57  0.75

I was expecting an output with 3 rows and 19 columns. What's happened here that I'm not understanding?

Any help would be appreciated!

You should use as.matrix instead of matrix to convert to matrix, also this can be done in fewer steps.

Xmatrix <- subset(questionOneA, select = -Y)
Xmatrix <- as.matrix(Xmatrix)
Xtranspose <- t(Xmatrix)

Xmatrix
#    Wt LvrWt Dose
#1  176   6.5 0.88
#2  176   9.5 0.88
#3  190   9.0 1.00
#4  176   8.9 0.88
#5  200   7.2 1.00
#6  167   8.9 0.83
#7  188   8.0 0.94
#8  195  10.0 0.98
#9  176   8.0 0.88
#10 165   7.9 0.84
#11 158   6.9 0.80
#12 148   7.3 0.74
#13 149   5.2 0.75
#14 163   8.4 0.81
#15 170   7.2 0.85
#16 186   6.8 0.94
#17 146   7.3 0.73
#18 181   9.0 0.90
#19 149   6.4 0.75

Xtranspose

#           1      2   3      4     5      6      7      8
#Wt    176.00 176.00 190 176.00 200.0 167.00 188.00 195.00
#LvrWt   6.50   9.50   9   8.90   7.2   8.90   8.00  10.00
#Dose    0.88   0.88   1   0.88   1.0   0.83   0.94   0.98
#           9     10    11     12     13     14     15     16
#Wt    176.00 165.00 158.0 148.00 149.00 163.00 170.00 186.00
#LvrWt   8.00   7.90   6.9   7.30   5.20   8.40   7.20   6.80
#Dose    0.88   0.84   0.8   0.74   0.75   0.81   0.85   0.94
#          17    18     19
#Wt    146.00 181.0 149.00
#LvrWt   7.30   9.0   6.40
#Dose    0.73   0.9   0.75

See what matrix(Xmatrix) returns:

matrix(Xmatrix)

#           [,1]      
#[1,] Integer,19
#[2,] Numeric,19
#[3,] Numeric,19

Just check the output from each of your steps, and you will see the matrix becomes a "one column" matrix after this step:

Xtranspose <- matrix(Xtranspose)

This function creates a matrix. If you see the manual of the matrix function you will see that it defaults to nrow=1 and ncol=1 .

Your matrix obviously has more elements than would fit in a 1x1 matrix, but creating a matrix isn't really what you would want to do at this point, you would just make sure that the 2-dimensional structure you have, is a matrix, for which as.matrix is better. (But unecessary, it already is a matrix.)

Though I will say, the manual does not explain this specific happening well enough. It does not clearly say what happens if you give matrix() a matrix as input data that has more elements than would fit in the given number of rows and columns you want.

Though it does say this, which is probably applicable to your case:

When coercing a vector, it produces a one-column matrix, and promotes the names (if any) of the vector to the rownames of the matrix.

This is also what you see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM