I have a nested data table called ldt. The highest dimension in each direction is
ldt[[44]][7200,4]
Meaning each nested data.table (DT) is 7200 rows by 4 columns, and there are 44 such nested DTs.
I want to create a second set of DTs based on the first.
Specifically, I want to execute the command
for (i in length(ldt)) {ldtSUM[[i]]<-ldt[[i]][, sum(V4), by="V1,V3"]}
If successful, this should generate a nested array of dimensionality
ldtSUM[[44]][120,4]
but instead doing so results in the error:
Error in ldtSUM[[i]] <- ldt[[i]][, sum(V4), by = "V1,V3"] : object 'ldtSUM' not found
presumably because I never initialized it.
So, to circumvent this, I attempted a variety of initialization techniques such as
ldtSUM=ldt[[1:44]][0,]
and many other statements, but they all failed for a variety of reasons, such as that I did not specify a number of empty rows, or "recursive indexing failed at level 3" etc.
So, in summary, what I would like to do is create a data.table wherein $V4 is summed by $V1,$V3 but I cannot seem to do this due to indexing and initialization issues.
Thank you very much!
根据agstudy的答案,正确的代码是:
ldtSUM<-lapply(ldt,function(x)x[, sum(V4), by = "V1,V3"]
You had two parts to your problem: The first part you have already solved by:
ldtSUM<-lapply(ldt,function(x)x[, sum(V4), by = "V1,V3"]
The second part is on initialization, and that can be solved by following:
ldtSUM <- list()
After this initialization if you run your original code:
for (i in length(ldt)) {ldtSUM[[i]]<-ldt[[i]][, sum(V4), by="V1,V3"]}
should also work.
Note: For your problem, the solution suggested by agstudy is best. But the initialization is a general trick that is useful in variety of situations.
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.