Home > Back-end >  Using split() to sum only for individuals first entry
Using split() to sum only for individuals first entry

Time:10-06

Consider the following sample data. The data has 2 individuals per group and each individual has 2 entries. Sample data

The code below computes sum of Func per group using split() function. I would like a code that will split the data and sum Func only for the first entry per individual (i.e sum for the red highlighted entries only).

Func<-X*Y Z
GroupSum<-as.numeric( sapply( split(Func,group),sum)  ) # Group sum of X*Y Z
sum(GroupSum) # Total fum of X*Y Z over all entries.

CodePudding user response:

We may use a group by approach i.e. grouped by 'group', slice the first row, ungroup, and then summarise to get the sum of X multiplied by 'Y' and added to 'Z'

library(dplyr)
df1 %>%
   group_by(group) %>%
   slice_head(n = 1) %>%
    ungroup %>%
   summarise(out = sum(X * Y   Z, na.rm = TRUE))

Or can use duplicated in base R

with(subset(df1, !duplicated(group)), sum(X * Y   Z, na.rm = TRUE))
  •  Tags:  
  • r
  • Related