I am trying to find, where my mistake is.
From multiple excel files (each have Quality sheet and Quantity sheet), each file has one sample of data, I import them (with read_excel to two different lists:
quant_list <- lapply(temp, function(x) read_excel(x,sheet = 'Quant_data'))
qualit_list <- lapply(temp, function(x) read_excel(x,sheet = 'Qualit_data'))
Then I need to collect some data from these lists and put them into a data.table In rows there are samples, in columns there parameters, some parameters are collected from qualit_list, some from quant_list
I've made a function, which makes a dt from each sample:
grab_fun <- function(qualit_list, quant_list_list) {
data.table(Param1 = qualit_list[13:13, 7][[1]],
Param2 = qualit_list[17:17, 7][[1]],
Param3 = qualit_list[22:22, 7][[1]],
Param4 = quant_list[10:10, 9][[1]])
}
It takes first three params from qualit_list, and next params from quant_list
if I try the function on on one sample of data, it successfully returns a one row dt:
grab_fun(qualit_list[[1]], quant_list[[1]])
But if I try this on the whole data:
result_dt <- rbindlist(lapply(qualit_list, grab_fun, quant_list = quant_list))
It is interesting, that if I exclude a Param4 from the function grab_fun, so that it only uses data from qualit_list, then the rbindlist with lapply works ok.
grab_fun <- function(qualit_list, quant_list_list) {
data.table(Param1 = qualit_list[13:13, 7][[1]],
Param2 = qualit_list[17:17, 7][[1]],
Param3 = qualit_list[22:22, 7][[1]])
}
Where am I wrong?
The easiest approach may be:
rbindlist(Map(grab_fun, qualit_list, quant_list))
As for your error above, this is caused because you are inadvertantly passing the entire set of Quantity Sheets (i.e. the entire quant_list
list) to the grab_fun
for each element of qualit_list
.
If trying to do this with lapply, you could use seq_along
instead, like this:
rbindlist(
lapply(seq_along(qualit_list), \(i) grab_fun(qualit_list[[i]], quant_list[[i]]))
)