0

Im currently trying to calculate change in amount of SWE for multiple sites (50) so as to attain snow melt rates (change per day). I think I have the right formula but trying to loop it doesn't seem to be working, my code and error below.

site <- colnames(winter_pr_swe[,55:104])
 for (i in site){
 sitex <- paste0(i)
 ratex <- paste0('rate',i)
 winter_pr_swe1 <- winter_pr_swe %>% mutate(ratex = (sitex - lag(sitex)) 
 }

Error: unexpected '}' in: "winter_pr_swe1 <- winter_pr_swe %>% %>% mutate(ratex = (sitex - lag(sitex)) 
}"

Here is the head of the data set for reference, each column being the SWE levels for a single site, each row being a days measurement:

 head(winter_pr_swe[,55:104])

 X303 X308 X310 X316 X327 X337 X340 X378 X386 X409 X416 X426
 1  157   71  234  287  361  163   86  292  241  348  109  183
 2  157  150  272  333  384  165  102  300  302  409  130  185
 3  157  150  274  340  396  168  107  310  312  419  130  190
 4  157  150  274  343  406  168  157  315  312  419  122  190
 5  157  147  274  351  409  168  193  318  318  434  107  190
 6  165  140  274  351  427  168  193  335  318  434  107  234
   X431 X465 X486 X488 X491 X511 X519 X527 X532 X538 X580 X586
 1  592  178   69   53  292  427   64  137  450  175  312  246
 2  658  208   97  122  340  490  155  137  508  190  340  282
 3  668  218   97  124  351  490  157  137  513  203  343  300
 4  676  218   94  124  351  490  157  152  521  203  343  302
 5  696  224   86  119  351  490  152  160  531  213  353  323
 6  726  226   79  109  351  485  145  168  549  218  368  333
   X589 X595 X617 X622 X624 X629 X632 X640 X665 X705 X715 X737
 1  310   41  351  300  315  206  358   66   46  358  157  478
 2  338   43  406  320  330  231  391  155   48  483  170  511
 3  353   33  406  328  338  241  396  155   48  485  180  521
 4  356   30  406  330  338  241  399  155   48  485  183  523
 5  363   25  401  333  353  267  427  155   56  485  183  556
 6  368   15  401  345  376  267  460  145   56  485  198  599
   X739 X755 X757 X762 X780 X827 X839 X840 X843 X857 X861 X866
 1  168  201  274  254  462  356   74  526  183  180   58  112
 2  196  244  310  257  533  363   74  584  224  196  145  160
 3  201  249  315  267  549  376   74  589  226  196  145  175
 4  206  251  318  267  551  376   74  592  229  196  145  160
 5  226  249  320  282  587  376   91  620  239  201  142  155
 6  226  249  320  282  599  417   91  648  241  201  119  145
  X874 X877
 1  615   23
 2  671  152
 3  678  155
4  681  155
5  709  140
6  752  140
benson23
  • 16,369
  • 9
  • 19
  • 38
  • FYI: the two pipings '%>%' in the error message are just a mistake from copying over the lines – Neil Langan Jul 25 '20 at 15:04
  • You're missing a closing parenthesis around `mutate(ratex = (sitex - lag(sitex))`. That is, it should be `mutate(ratex = (sitex - lag(sitex)))` – duckmayr Jul 25 '20 at 15:05
  • oh cheers, thatsworked however now im greeted with Error: Problem with `mutate()` input `ratex`. x non-numeric argument to binary operator ℹ Input `ratex` is `(sitex - lag(sitex))`. – Neil Langan Jul 25 '20 at 15:13
  • That one's because you're trying to subtract characters... You may want to check out https://dplyr.tidyverse.org/articles/programming.html – duckmayr Jul 25 '20 at 15:14
  • why when using the if loop and paste0 function do they become characters rather than merely column names? sorry, as im sure you can tell, I am very new to all this – Neil Langan Jul 25 '20 at 15:43
  • This is a common thing that bites people who are new to `dplyr`; the package uses indirection, so that when you do `sitex - lag(sitex)` it literally uses a variable named `sitex` in your data rather than a variable with the name of the value stored in `sitex`; if such a variable cannot be found in your data, it may be found (as here) in the calling environment. So, it tries to subtract from the value stored in `sitex` (which is a character vector) the value of `lag(sitex)` (which will be `NA` since `sitex` is of length one). – duckmayr Jul 25 '20 at 15:52
  • You can see [this answer](https://stackoverflow.com/a/48219802/8386140) to a related question for more details, and how to fix this – duckmayr Jul 25 '20 at 15:52

0 Answers0