I have a 32X32 matrix with the columns and rows representing each NFL team. Running across the rows indicates on average how much that team beat their opponent (indicated by the column name).
I created 32 vectors for each team:
nfl_data <- read.csv(file="2012NFLScoresMatrixNoNames.csv", h=T)
colnames(nfl_data) <- NULL
ari_s <- nfl_data[1,]
atl_s <- nfl_data[2,]
.
.
.
was_s <- nfl_data[32,]
a <- rbind(c(ari_s),c(atl_s),c(bal_s),c(buf_s),c(car_s),c(chi_s),c(cin_s),
c(cle_s),c(dal_s),c(den_s),c(det_s),c(grn_s),c(hou_s),c(ind_s),
c(jac_s),c(kc_s),c(mia_s),c(min_s),c(ne_s),c(no_s),c(nyj_s),
c(nyg_s),c(oak_s),c(phi_s),c(pit_s),c(sd_s),c(sea_s),c(sf_s),
c(stl_s),c(tb_s),c(tn_s),c(was_s))
a_trans <- t(a)
x <- solve(a_trans)
This is where i get an error:
Error in solve.default(a_trans) : 'a' must be a numeric matrix
> str(nfl_data)
'data.frame': 32 obs. of 32 variables:
$ : num 0 4 0 3 0 15 0 0 0 0 ...
$ : int -4 0 0 0 4 0 0 0 -6 -6 ...
$ : num 0 0 0 0 0 0 -12.5 -8.5 -2 17 ...
$ : num -3 0 0 0 0 0 0 -10 0 0 ...
$ : int 0 -4 0 0 0 1 0 0 5 22 ...
$ : num -15 0 0 0 -1 0 0 0 -16 0 ...
$ : num 0 0 12.5 0 0 0 0 1.5 1 8 ...
$ : num 0 0 8.5 10 0 0 -1.5 0 3 22 ...
$ : num 0 6 2 0 -5 16 -1 -3 0 0 ...
$ : num 0 6 -17 0 -22 0 -8 -22 0 0 ...
$ : num 28 13 0 0 0 4 0 0 0 0 ...
$ : num -14 0 0 0 0 -10.5 0 0 0 0 ...
$ : int 0 0 -30 -12 0 -7 0 0 0 -6 ...
$ : int 0 0 0 -7 0 20 0 -4 0 0 ...
$ : num 0 0 0 16 0 38 17 0 0 0 ...
$ : num 0 16 3 18 -6 0 22 23 0 21.5 ...
$ : num 3 0 0 -4.5 0 0 -4 0 0 0 ...
$ : num -7 0 0 0 0 5 0 0 0 0 ...
$ : num 2 0 1 -15 0 0 0 0 0 -10 ...
$ : int 0 3 0 0 7 0 0 0 -3 10 ...
$ : num -1 0 0 -0.5 0 0 0 0 0 0 ...
$ : num 0 34 19 0 -29 0 18 -14 1 0 ...
$ : num 0 3 35 0 11 0 24 3 0 22 ...
$ : num 21 13 -1 0 8 0 21 -1 10 0 ...
$ : int 0 0 0 0 0 0 -2 -4 3 12 ...
$ : num 0 24 0 0 26 0 7 1 0 9 ...
$ : num -27 0 0 -33 -4 -6 0 0 -20 0 ...
$ : num -17.5 0 0 -42 0 -25 0 0 0 0 ...
$ : num -14 0 0 -3 0 17 0 0 0 0 ...
$ : int 0 -2 0 0 -6 0 0 0 6 8 ...
$ : num 0 0 0 -1 0 31 0 0 0 0 ...
$ : num 0 7 -3 0 8 0 7 -17 -8.5 0 ...
> str(a)
List of 1024
$ : num 0
$ : num 4
$ : num 0
$ : num 3
$ : num 0
$ : num 15
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num -28
$ : num 14
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num -3
$ : num 7
$ : num -2
$ : num 0
$ : num 1
$ : num 0
$ : num 0
$ : num -21
$ : num 0
$ : num 0
$ : num 27
$ : num 17.5
$ : num 14
$ : num 0
$ : num 0
$ : num 0
$ : int -4
$ : int 0
$ : int 0
$ : int 0
$ : int 4
$ : int 0
$ : int 0
$ : int 0
$ : int -6
$ : int -6
$ : int -13
$ : int 0
$ : int 0
$ : int 0
$ : int 0
$ : int -16
$ : int 0
$ : int 0
$ : int 0
$ : int -3
$ : int 0
$ : int -34
$ : int -3
$ : int -13
$ : int 0
$ : int -24
$ : int 0
$ : int 0
$ : int 0
$ : int 2
$ : int 0
$ : int -7
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num -12.5
$ : num -8.5
$ : num -2
$ : num 17
$ : num 0
$ : num 0
$ : num 30
$ : num 0
$ : num 0
$ : num -3
$ : num 0
$ : num 0
$ : num -1
$ : num 0
$ : num 0
$ : num -19
$ : num -35
$ : num 1
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 0
$ : num 3
$ : num -3
$ : num 0
$ : num 0
[list output truncated]
- attr(*, "dim")= int [1:2] 32 32
I looked over and over to see if I had a comma or maybe an o instead of a 0, but I couldn't find anything. Any suggestions on how to solve this?
The object a
is a list, not a matrix or data frame. Have a look at your code:
a <- rbind(c(ari_s),c(atl_s),c(bal_s),c(buf_s),c(car_s),c(chi_s),c(cin_s),
c(cle_s),c(dal_s),c(den_s),c(det_s),c(grn_s),c(hou_s),c(ind_s),
c(jac_s),c(kc_s),c(mia_s),c(min_s),c(ne_s),c(no_s),c(nyj_s),
c(nyg_s),c(oak_s),c(phi_s),c(pit_s),c(sd_s),c(sea_s),c(sf_s),
c(stl_s),c(tb_s),c(tn_s),c(was_s))
Here, you used c
for each object. If you omit the c
function calls, a
will be a data frame, not a common list.
A small example:
> x <- data.frame(a=1:2, b=0.4)
> x1 <- x[1,]
> x2 <- x[2,]
> str(rbind(c(x1), c(x2)))
List of 4
$ : int 1
$ : int 2
$ : num 0.4
$ : num 0.4
- attr(*, "dim")= int [1:2] 2 2
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "a" "b"
> str(rbind(x1, x2))
'data.frame': 2 obs. of 2 variables:
$ a: int 1 2
$ b: num 0.4 0.4
The function c
transforms the object of class data.frame
into an object of class list
.
Maybe it's much easier to use
solve(t(nfl_data))
The data frame nfl_data
will be converted into a matrix due to t
.
The informative bit when you do str(a)
is this:
> str(a)
List of 1024
....
In other words, a
is a list, not a matrix. The "numeric" qualifier in the error message 'a' must be a numeric matrix
is there to stop people complaining when a user tries to invert the other types of matrix (logical and character). In your case, the error message means: " You didn't give me a matrix of any sort ".
The easiest way to solve this is just to transpose the data frame:
solve(t(nfl_data))
where the use of t()
will convert the nfl_data
data frame to a matrix via the as.matrix()
coercion function. To be more explicit and if you data might include factors, then you may wish to convert yourself via data.matrix()
, eg
solve(t(data.matrix(nfl_data)))
But either way, there is no need to strip out by hand each row-vector from the data frame, bind these row-wise and then transpose.
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.