Summing across rows of a data.table for specific columns

后端 未结 2 1580
日久生厌
日久生厌 2020-12-15 07:40

I have a large data table (from the package data.table) with over 60 columns (the first three corresponding to factors and the remaining to response variables, in this case

2条回答
  •  天涯浪人
    2020-12-15 07:44

    An alternative (data.table) approach would be to store your data in long form. Version 1.8.11 of data.table has fast melt and dcast methods

    library(reshape2)
    mt <- melt(test, id=1:3,variable.name='Species')
    
    abundance <- mt[,list(abundance = mean(value)),by=list(Zone,quadrat,Species)][, 
                    sumAbundance := sum(abundance), by = list(Zone,quadrat)]
    

    Working in long format will take a slight change in thinking, but it may end up being more efficient memory wise (as less internal copying will be involved, and you are referencing a single not multiple elements within every "by" group.)

提交回复
热议问题