Ho una matrice che viene presentata qui attraverso il dput()
funzione.
Matrix <- structure(list(Int = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), Time = c(31, 34, 6, 48, 36, 46, 20, 45, 49, 8, 40,
28, 14, 13, 22, 30, 21, 15, 8, 16, 22, 47, 31, 27, 25, 43, 9,
24, 20, 7, 48, 10, 48, 18, 44, 26, 43, 39, 44, 41, 20, 36, 43,
4, 30, 16, 41, 2, 49, 25, 43, 5, 31, 15, 16, 20, 11, 16, 20,
14, 48, 26, 44, 4, 29, 42, 41, 14, 47, 21, 25, 11, 41, 20, 48,
35, 4, 42, 19, 8, 34, 23, 39, 25, 11, 29, 24, 31, 9, 20, 6, 45,
46, 23, 32, 12, 23, 11, 7, 39), Binary = c(1, 1, 1, 1, 1, 1,
0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
0, 1, 1, 0, 1, 0, 0, 0, 0, 1), IndID = structure(c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L), .Label = c("AAA", "BBB", "CCC", "DDD"), class = "factor")), .Names = c("Int",
"Time", "Binary", "IndID"), class = "data.frame", row.names = c(NA,
-100L))
head(Matrix)
Int Time Binary IndID
1 1 31 1 AAA
2 1 34 1 BBB
3 1 6 1 CCC
4 1 48 1 DDD
5 1 36 1 AAA
6 1 46 1 BBB
Matrix ha 100 righe e 4 colonne. Le prime tre colonne sono necessarie per la matematica. La 4a colonna è usata come un ID. Voglio moltiplicare la matrice vettoriale (sotto) per le prime tre colonne della matrice più grande per ogni individuo.
Ecco la matrice vettoriale ...
coef <- c(-0.13731791, -0.01122941, 0.65487107)
coef <- as.matrix(coef)
coef
Sto cercando di usare il codice qui sotto.
Matrix[Matrix$IndID=="AAA", c(1,2,3)] %*% coef
In parole: moltiplicare le colonne 1: 3 di Matrix per coef quando la riga INDID contiene "AAA". Ciò si traduce nel seguente errore.
"Error in Matrix[Matrix$IndID == "AAA", c(1, 2, 3)] %*% coef :
requires numeric/complex matrix/vector arguments"
Ho anche provato a combinare due condizionali nelle colonne condizionali usando l'operatore & come avevo visto su altri post SO. Quel codice e l'errore risultante sono sotto.
Matrix[,(Matrix$IndID=="AAA") & (c(1,2,3))] %*% coef
Error in `[.data.frame`(Matrix, , (Matrix$IndID == "AAA") & (c(1, 2, 3))) :
undefined columns selected
In addition: Warning message:
In (Matrix$IndID == "AAA") & (c(1, 2, 3)) :
longer object length is not a multiple of shorter object length
Questi dati sono un esempio dei miei dati reali in cui sto cercando di aggiungere queste funzioni in un più grande for()
loop con molte persone.
Grazie per eventuali suggerimenti!
risposte:
0 per risposta № 1Matrix
è un frame di dati, non una matrice. Puoi coercire il sottoinsieme di Matrix
vuoi fare matematica con as.matrix()
. Provare:
as.matrix(Matrix[Matrix$IndID=="AAA", c(1,2,3)]) %*% coef