title: R: Using dplyr to create a dataframe with percentage per group
body: <p>I'm having trouble trying to create a new dataframe that creates a percentage per year with dplyr.</p>
  
  <p>Dataframe is as follows:</p>
  
  <pre><code>    structure(list(orgid = c("USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
  "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
  "USGS-PA", "USGS-PA", "USGS-NJ", "USGS-NJ", "USGS-NJ"), stdate = structure(c(16134, 
  16133, 16135, 16133, 16105, 15749, 16112, 16394, 16610, 16610, 
  16511, 16560, 16566, 16328, 16324), class = "Date"), locid = c("USGS-01367785", 
  "USGS-01455099", "USGS-01440000", "USGS-01380100", "USGS-01398000", 
  "USGS-01461880", "USGS-0140940950", "USGS-01482500", "USGS-0146453250", 
  "USGS-0146453250", "USGS-01444800", "USGS-01444800", "USGS-01477120", 
  "USGS-01392150", "USGS-01376274"), charnam = c("Total dissolved solids", 
  "Total dissolved solids", "Total dissolved solids", "Total dissolved solids", 
  "Total dissolved solids", "Total dissolved solids", "Total dissolved solids", 
  "Total dissolved solids", "Total dissolved solids", "Total dissolved solids", 
  "Total dissolved solids", "Total dissolved solids", "Total dissolved solids", 
  "Total dissolved solids", "Total dissolved solids"), val = c("154", 
  "333", "109", "143", "711", "218", "104", "157", "506", "471", 
  "3040", "1110", "142", "429", "266")), .Names = c("orgid", "stdate", 
  "locid", "charnam", "val"), row.names = c(NA, 15L), class = "data.frame")
  </code></pre>
  
  <p>I want to create a new column with the percentage of each year's Total dissolved solids that is > 500.</p>
  
  <p>Code I have so far:</p>
  
  <pre><code>if (!require(pacman)) {
    install.packages('pacman')
  
  }
  
  pacman::p_load("ggplot2","tidyr","plyr","dplyr")
  #### Read in the necessary data ######
  roadsalt_data&lt;-read.table("QADportaldata_1988-2015.tsv",header=T,sep="\t",fill=T,stringsAsFactors = F)
  #Convert date column from a character class to a date class so ggplot can  display as a continuous variable ###
  roadsalt_data$stdate &lt;- as.Date(roadsalt_data$stdate)
  ## Filter dataset to only contain columns I need ########
  filtered_roadsalt &lt;- roadsalt_data %&gt;% 
    select(orgid, stdate,locid, charnam,val) %&gt;%
    filter(between(stdate, as.Date("1996-01-01"), as.Date("2015-07-01"))) %&gt;%
    filter(charnam == "Total dissolved solids" &amp; as.numeric(as.character(val)) &gt; 50.00)
  ##create a dataframe for percent of TDS &gt;500
  percent_data&lt;-filtered_roadsalt %&gt;%
    mutate(year=as.Date(cut(stdate, breaks = "year"))) %&gt;%
    group_by(year) %&gt;%
    mutate(prop = round(as.numeric(as.character(val))/sum(as.numeric(as.character(val)))*100, 2))
  </code></pre>
  
  <p>This however is not getting me the results I'm after.. The dataframe I want should have 19 observations and 2 variables. The 19 observations being each year from 1997-2015 and there percentage. Any help would be greatly appreciated! Thanks!</p>