计算百分比变化并将其放在表格中

时间:2019-06-12 08:32:28

标签: r dplyr

我有一个包含三列的数据框,一列是年份,其余的只是匿名数据。我想计算多年来的百分比变化并将其放在表格中。看看。

 webView.evaluateJavaScript("document.getElementsByName('form1').value") { (result, error) in
        print(result as Any)
        print(error as Any)
    }

我能够计算出百分比变化

#here's my data
data<-structure(list(Year = c(2012, 2012, 2012, 2012, 2012, 2012, 2012, 
                          2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 
                          2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 
                          2013, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
                          2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 
                          2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 
                          2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
                          2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
                          2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017), V2 = c("O1", 
                                                                                        "O2", "O3", "O4", "O5", "O6", "O7", "O8", "O9", "O10", "O12", 
                                                                                        "O13", "O14", "O15", "O16", "O1", "O2", "O3", "O4", "O5", "O6", 
                                                                                        "O7", "O8", "O9", "O10", "O12", "O13", "O14", "O15", "O16", "O1", 
                                                                                        "O2", "O3", "O4", "O5", "O6", "O7", "O8", "O9", "O10", "O12", 
                                                                                        "O13", "O14", "O15", "O16", "O1", "O2", "O3", "O4", "O5", "O6", 
                                                                                        "O7", "O8", "O9", "O10", "O11", "O12", "O13", "O14", "O15", "O16", 
                                                                                        "O1", "O2", "O3", "O4", "O5", "O6", "O7", "O8", "O9", "O10", 
                                                                                        "O11", "O12", "O13", "O14", "O15", "O16", "O1", "O2", "O3", "O4", 
                                                                                        "O5", "O6", "O7", "O8", "O9", "O10", "O11", "O12", "O13", "O14", 
                                                                                        "O15", "O16"), KOW = c(778.4, 37586.5, 6160.7, 260.9, 50812.1, 
                                                                                                               24419.6, 30179.4, 2733.2, 59518.7, 14696, 20926.6, 35195.2, 21446.7, 
                                                                                                               5068.9, 7276.2, 1014.1, 38691, 6765.8, 154.8, 43218.9, 31110.8, 
                                                                                                               20114.9, 56.7, 69116.4, 13787.4, 13483.3, 33735.9, 22899.1, 2977.8, 
                                                                                                               5127.1, 926.4, 61612.4, 7532.3, 194.9, 41324.9, 38248.7, 28609.8, 
                                                                                                               959.3, 58435.6, 16506.3, 11754.4, 40769, 30958.6, 3408.2, 5354.5, 
                                                                                                               1355.4, 58311.9, 9413.2, 206.2, 42858.4, 44511.2, 24667.4, 78.2, 
                                                                                                               66650.9, 16220.4, 17, 11940.6, 38096.1, 35791.9, 2949.3, 8220.7, 
                                                                                                               826.6, 66431.7, 15258.7, 286.4, 47960.6, 38507.8, 31062.2, 773.6, 
                                                                                                               73581.2, 19931.6, 0.5, 14735.4, 38361.2, 39286.8, 2904.3, 7993.1, 
                                                                                                               576.4, 66232, 16049.1, 335.2, 46367.2, 37431.9, 25392.7, 2232, 
                                                                                                               81797.7, 22793.7, 0.5, 16569.4, 37323, 55369.2, 3666, 6604.7)), row.names = c(NA, 
                                                                                                                                                                                             -93L), vars = "Year", drop = TRUE, class = c("grouped_df", "tbl_df", 
                                                                                                                                                                                                                                          "tbl", "data.frame"))


#Year   V2  KOW

#2012   O1  778.4
#2012   O2  37586.5
#2012   O3  6160.7
#2012   O4  260.9
#2012   O5  50812.1
#2012   O6  24419.6
#2012   O7  30179.4
             .
             .
             .

我不能做的就是以以下格式将其放入表格中, 省略了2012年

pct_chg<-data%>%
  group_by(V2)%>%
  arrange(Year, .by_group=TRUE)%>%
  mutate("+/-" = (KOW/lag(KOW)-1)*100)

#Year   V2  KOW       +/-

#2012   O1  778.4     NA
#2013   O1  1014.1  30.28
#2014   O1  926.4   -8.65
#2015   O1  1355.4  46.31
                   .
                   .
                   .

0 个答案:

没有答案