r - Combine rows and sum their values -
below there fraction of original data frame. need combine rows in specific id repeated in specific season , lic , vessel different. combining need sum qtty , grosston.
please take id 431 in season 1998 example (*).
season lic id qtty vessel grosston … 1998 16350 431 40 435 57 1998 16353 431 28 303 22.54 … the same subject 431 has 2 different lic (16350 & 16353) , 2 different vessels (435 & 303). result expected in specific case is:
season lic id qtty vessel grosston … 1998 16350 431 68 435 79.54 … i don't mind lic , vessel remind in resulting row, want keep season, id , resulting sum of qtty , grosston. in above example manually chose lic 16350 , vessel 435.
to honest have no idea do, i'd appreciate help.
thanks
original data (*= rows combined)
season lic id qtty vessel grosston 1998 15593 411 40 2643 31.5 1999 27271 411 40 2643 31.5 2000 35758 411 40 2643 31.5 2001 45047 411 50 2643 31.5 2002 56291 411 55 2643 31.5 2003 66991 411 55 2643 31.5 2004 80581 411 55 2643 31.5 2005 95058 411 52 na na 2006 113379 411 50 10911 4.65 2007 120894 411 50 10911 4.65 2008 130033 411 50 2483 8.5 2009 139201 411 46 2296 50 2010 148833 411 46 2296 50 2011 158395 411 46 2296 50 1998 16350 431 40 435 57 # * 1998 16353 431 28 303 22.54 # * 2000 37491 436 50 2021 19.11 2001 47019 436 50 2021 19.11 2002 57588 436 51 2021 19.11 2003 69128 436 51 2021 19.11 2004 82400 436 52 2021 19.11 2005 95599 436 50 2021 19.11 2006 113126 436 50 2021 19.11 2007 122387 436 50 2021 19.11 2008 131126 436 50 2021 19.11 2009 140417 436 50 2021 19.11 2010 150673 436 50 2021 19.11 2011 159776 436 50 2021 19.11 also need keep previous , following rows have 1 id per season. this: (*=row resulting after being combined)
season lic id qtty vessel grosston 1998 15593 411 40 2643 31.5 1999 27271 411 40 2643 31.5 2000 35758 411 40 2643 31.5 2001 45047 411 50 2643 31.5 2002 56291 411 55 2643 31.5 2003 66991 411 55 2643 31.5 2004 80581 411 55 2643 31.5 2005 95058 411 52 na na 2006 113379 411 50 10911 4.65 2007 120894 411 50 10911 4.65 2008 130033 411 50 2483 8.5 2009 139201 411 46 2296 50 2010 148833 411 46 2296 50 2011 158395 411 46 2296 50 1998 16350 431 68 435 79.54 #* 2000 37491 436 50 2021 19.11 2001 47019 436 50 2021 19.11 2002 57588 436 51 2021 19.11 2003 69128 436 51 2021 19.11 2004 82400 436 52 2021 19.11 2005 95599 436 50 2021 19.11 2006 113126 436 50 2021 19.11 2007 122387 436 50 2021 19.11 2008 131126 436 50 2021 19.11 2009 140417 436 50 2021 19.11 2010 150673 436 50 2021 19.11 2011 159776 436 50 2021 19.11
if turn data.frame data.table can make great use of by argument
library(data.table) dt <- data.table(df) # df original data then 1 line:
dt[, lapply(.sd, sum), by=list(season, lic, id, vessel)] we can filter 1998 season, if we'd like: '
dt[, lapply(.sd, sum), by=list(season, lic, id, vessel)][season==1998] season lic id vessel qtty grosston 1: 1998 15593 411 2643 40 31.50 2: 1998 16350 431 435 68 114.00 3: 1998 16353 431 303 68 45.08 the entire result output looks this:
season lic id vessel qtty grosston 1: 1998 15593 411 2643 40 31.50 2: 1999 27271 411 2643 40 31.50 3: 2000 35758 411 2643 40 31.50 4: 2001 45047 411 2643 50 31.50 5: 2002 56291 411 2643 55 31.50 6: 2003 66991 411 2643 55 31.50 7: 2004 80581 411 2643 55 31.50 8: 2005 95058 411 na 52 na 9: 2006 113379 411 10911 50 4.65 10: 2007 120894 411 10911 50 4.65 11: 2008 130033 411 2483 50 8.50 12: 2009 139201 411 2296 46 50.00 13: 2010 148833 411 2296 46 50.00 14: 2011 158395 411 2296 46 50.00 15: 1998 16350 431 435 68 114.00 16: 1998 16353 431 303 68 45.08 17: 1999 28641 431 303 68 45.08 18: 1999 28644 431 435 68 114.00 19: 2000 37491 436 2021 50 19.11 20: 2001 47019 436 2021 50 19.11 21: 2002 57588 436 2021 51 19.11 22: 2003 69128 436 2021 51 19.11 23: 2004 82400 436 2021 52 19.11 24: 2005 95599 436 2021 50 19.11 25: 2006 113126 436 2021 50 19.11 26: 2007 122387 436 2021 50 19.11 27: 2008 131126 436 2021 50 19.11 28: 2009 140417 436 2021 50 19.11 29: 2010 150673 436 2021 50 19.11 30: 2011 159776 436 2021 50 19.11 season lic id vessel qtty grosston
Comments
Post a Comment