group by and summarize with conditional sum

I have a vegetation survey dataset where each row represents one species identified within a plot. I need to summarize this data by plot #, and generate fields for total # of species per plot, native species per plot, nonnative species per plot, total % cover per plot, native % cover per plot, and nonnative % cover per plot. I've grouped by plot and used summarize to generate these fields. Everything is working except the native % cover and nonnative % cover. It’s not giving me an error but it’s returning weird numbers that don’t add up. The first plot (B1) looks right, but every other plot is wrong, and I can’t figure out why or what it’s doing.

A sample of my data looks like:

Plot# num Aspect (Bottom, North, South/West, Top/East) Date Surveyor Seed Mix (high/low) Annual (Yes/No) Biochar (Yes/No) Species % Cover Native_Status Photo (yes/no) Note Sci_Name Com_Name Duration Family Habit
B1 1 Bottom 45058 JT High No No ANTHCOT 75 L48 (I) Anthemis cotula L. stinking chamomile Annual Asteraceae Forb/herb
B1 1 Bottom 45058 JT High No No ECHIVUL 10 L48 (I) Echium vulgare L. common viper's bugloss Annual, Biennial, Perennial Boraginaceae Forb/herb
B1 1 Bottom 45058 JT High No No LACTSER 4 L48 (I) Lactuca serriola L. prickly lettuce Annual, Biennial Asteraceae Forb/herb
B1 1 Bottom 45058 JT High No No FESTIDA 1 L48 (N) Festuca idahoensis Elmer Idaho fescue Perennial Poaceae Graminoid
B1 1 Bottom 45058 JT High No No AVENFAT 1 L48 (I) Avena fatua L. wild oat Annual Poaceae Graminoid
B1 1 Bottom 45058 JT High No No CLARPUL 0.5 L48 (N) Clarkia pulchella Pursh pinkfairies Annual Onagraceae Forb/herb
B1 1 Bottom 45058 JT High No No ACHIMIL 1 L48 (N) Achillea millefolium L. common yarrow Perennial Asteraceae Forb/herb
B1 1 Bottom 45058 JT High No No MADI1S1 0.5 L48 (N) Madia sp. tarweed 0 Asteraceae 0
B1 1 Bottom 45058 JT High No No SISYALT 30 L48 (I) Sisymbrium altissimum L. tall tumblemustard Annual, Biennial Brassicaceae Forb/herb
B2 2 Bottom 45058 JT High No Yes ACHIMIL 80 L48 (N) Achillea millefolium L. common yarrow Perennial Asteraceae Forb/herb
B2 2 Bottom 45058 JT High No Yes SISYALT 25 L48 (I) Sisymbrium altissimum L. tall tumblemustard Annual, Biennial Brassicaceae Forb/herb
B2 2 Bottom 45058 JT High No Yes ANTHCOT 12 L48 (I) Anthemis cotula L. stinking chamomile Annual Asteraceae Forb/herb
B2 2 Bottom 45058 JT High No Yes LUPISER 0.5 L48 (N) Lupinus sericeus Pursh silky lupine Perennial Fabaceae Subshrub, Forb/herb
B2 2 Bottom 45058 JT High No Yes CLARPUL 1 L48 (N) Clarkia pulchella Pursh pinkfairies Annual Onagraceae Forb/herb
B2 2 Bottom 45058 JT High No Yes LACTSER 3 L48 (I) Lactuca serriola L. prickly lettuce Annual, Biennial Asteraceae Forb/herb
B2 2 Bottom 45058 JT High No Yes ECHIVUL 5 L48 (I) Echium vulgare L. common viper's bugloss Annual, Biennial, Perennial Boraginaceae Forb/herb
B2 2 Bottom 45058 JT High No Yes ERIOHER 0.5 L48 (N) Eriogonum heracleoides Nutt. parsnipflower buckwheat Perennial Polygonaceae Subshrub, Forb/herb
B2 2 Bottom 45058 JT High No Yes AVENFAT 0.5 L48 (I) Avena fatua L. wild oat Annual Poaceae Graminoid
B2 2 Bottom 45058 JT High No Yes FESTIDA 0.5 L48 (N) Festuca idahoensis Elmer Idaho fescue Perennial Poaceae Graminoid

My code looks like:

#Generate summary table of plots. Sp_tot=# of species ID'd, sp_N=# of native species ID'd, sp_NonN=# 0f nonnative species ID'd, 
# Cov_Tot=total % cover, Cov_N=% cover of native species, Cov_NonN=% COver of nonnative species, Aspect=Aspect of plot, 
#Seed Mix=high/low diversity mix applied, biochar= biochar added yes or no, annual= annual seed mix added yes or no
Plot_Summary<-df %>%
  group_by(Plot.) %>%
  summarize(Sp_Tot=sum(Species==Species, na.rm = TRUE),
            sp_N=sum(Native_Status=='L48 (N)', na.rm = TRUE),
            sp_NonN=sum(Native_Status=='L48 (I)', na.rm = TRUE),
            Cov_Tot=sum(X..Cover, na.rm = TRUE),
            Cov_N=sum(df[which(Native_Status=='L48 (N)'),"X..Cover"], na.rm = TRUE), ##This isn't working right!!
            Cov_NonN=sum(df[which(Native_Status=='L48 (I)'), "X..Cover"], na.rm = TRUE), ##This isn't working right!!
            Aspect=first(Aspect..Bottom..North..South.West..Top.East.), 
            Seed_Mix=first(Seed.Mix..high.low.), 
            biochar=first(Biochar..Yes.No.),
            annual=first(Annual..Yes.No.)) %>%
  arrange(Plot.)  

The resulting data frame looks like:

Plot. Sp_Tot sp_N sp_NonN Cov_Tot Cov_N Cov_NonN Aspect Seed_Mix biochar annual
B1 9 4 5 123 3 120 Bottom High No No
B10 8 5 3 64.5 78 15 Bottom High No Yes
B11 8 5 3 75 78 15 Bottom High No No
B12 9 4 5 66.5 106.5 16.5 Bottom High Yes No
B13 11 6 5 54.5 185.5 42.5 Bottom Low No No
B14 9 4 4 74 107 15.5 Bottom Low Yes No
B15 10 7 2 77 109 14 Bottom Low Yes Yes
B16 10 5 5 44.5 107 96 Bottom Low No Yes
B17 11 8 3 97 117.5 110.5 Bottom High No Yes
B18 9 4 4 51.5 110 12 Bottom High Yes Yes
B19 9 5 4 112 117 6 Bottom High No No
B2 10 5 5 128 157.5 45.5 Bottom High Yes No
B20 11 6 5 111 108.5 119.5 Bottom High Yes No
B21 9 5 3 47.5 87.5 5.5 Bottom Low No Yes
B22 8 5 3 59.5 90.5 2.5 Bottom Low No No
B23 7 3 4 55.5 5.5 87 Bottom Low Yes No
B24 7 4 3 71 77.5 15 Bottom Low Yes Yes
B3 8 6 2 104.5 79 14 Bottom High No Yes
B4 9 6 3 47 111 12 Bottom High Yes Yes
B5 6 2 4 78.5 11 80.5 Bottom Low Yes No
B6 9 4 5 127.5 77 46 Bottom Low No No
B7 6 3 3 60.5 76.5 15 Bottom Low Yes Yes
B8 11 6 4 52.5 91.5 106.5 Bottom Low No Yes
B9 8 4 4 92 81 12 Bottom High Yes Yes
N1 12 9 3 24.5 155.5 84.5 Bottom Low Yes No
N10 11 6 5 33.5 116 112 North Low No No
N11 16 9 6 34 50.5 194 North Low No No
N12 15 8 6 49 123.5 117 North Low No Yes
N13 11 7 4 23.5 133.5 94.5 North High Yes Yes
N14 12 9 3 24.5 234.5 5.5 North High No Yes
N15 14 9 5 28.5 204 37.5 North High Yes No
N16 10 5 5 37.5 119.5 83.5 North High No No

Notice Cov_N and Cov_NonN don't add up to Cov_Tot, except for the very first plot. Can anyone tell me what I'm doing wrong and what I might do to fix it? Thanks!

You may see more answers if you edit to include a a reprex (see the FAQ. Although there is code here, it's not in usable form. To fix

dput(df)

and cut-and-paste the output enclosed in triple backtick code blocks.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.