Как применить к каждому i-му столбцу j-й матрицы в списке матриц - PullRequest
0 голосов
/ 13 июня 2018

Я пытаюсь написать некоторые функции, которые работают с матрицами в списке, так что конкретная операция выполняется для набора столбцов в каждой матрице для каждой матрицы в списке.Код касается проекта, включающего данные, которые не являются моими, поэтому я не могу поделиться точным кодом и данными.Тем не менее, вот некоторый код для воспроизводимого примера борьбы, которую я испытываю.Может кто-нибудь просветить меня о том, как выполнить i-й столбец в J-й матричной функции с помощью * apply?

 ### This will generate a list of data frames so there is an reproducible example.
list = lapply(seq(1:10), function(x) dplyr::sample_n(iris[,1:4], size=30))

### List the number of columns per matrix for ease
n_i = 4

### If I manually set the list index the function works fine (for that list). Let's suppose I want the residuals for all variables being predicted by Sepal.Width
get.residuals = function(i) {
 residuals(lm(y ~ ., data=cbind.data.frame(y=list[[5]][,i], list[[5]][,2])))
}

### This gives the residuals for each variable given all other variables. This doesn't make sense here of course.
sapply(c(1,3,4), function(i) get.residuals(i))

### Looks perfect. The output is the residuals for three variables specified in c(1,3,4) being predicted by Sepa.Width

### Now I want to extract the residuals for every column i in each matrix j in the list. I won't show them  all here, but I've tried probably 50 different variations of the functions and uses of lapply,sapply,apply,and for loops and just can't seem to get it to do what I'd like. 

### As an example of an attempt that thows the error "Error in terms.formula(formula, data = data) : '.' in formula and no 'data' argument

get.residuals = function(i,j) {
 residuals(lm(y ~ ., data=cbind.data.frame(y=list[[j]][,i], list[[j]][,2])))
}

lapply(list, function(j) get.residuals(i))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...