I try to write a loop for my Savitzky-Golay filter code, I have ten columns, and try to repeat them on each column with same process.
Sample dataset:
PLOT1500=structure(list(system = c("1-Jan-16", "2-Jan-16", "3-Jan-16",
"4-Jan-16"), evi1500 = c(0.437, 0.3891771, 0.493, 0.261), evi21500 = c(0.467436348,
0.186, 0.446531, 0.386620096), kndvi1500 = c(0.686, 0.413083514,
0.452461736, 0.508), ndvi1500 = c(0.482, 0.57, 0.461, 0.198),
nirv1500 = c(0.453765489, 0.624, 0.694, 0.457), evi2500 = c(0.439085339,
0.415, 0.447462363, 0.683), evi22500 = c(0.391, 0.715, 0.394061226,
0.382846651), kndvi2500 = c(0.452913701, 0.414950345, 0.420522988,
0.475), ndvi2500 = c(0.32139863, 0.515688255, 0.427, 0.427274026
), nirv2500 = c(0.491, 0.173, 0.444, 0.471)), row.names = c(NA,
4L), class = "data.frame")
code:
library(dplyr)
library(signal)
sg <- sgolay(p=7, n=11, m=0)
sevi1500 <- filter(sg, PLOT1500$evi1500)
sevi1500
sevi21500 <- filter(sg, PLOT1500$evi21500)
sevi21500
skndvi1500 <- filter(sg, PLOT1500$kndvi1500)
skndvi1500
sndvi1500 <- filter(sg, PLOT1500$ndvi1500)
sndvi1500
snirv1500 <- filter(sg, PLOT1500$nirv1500)
snirv1500
sevi2500 <- filter(sg, PLOT1500$evi2500)
sevi2500
sevi22500 <- filter(sg, PLOT1500$evi22500)
sevi22500
skndvi2500 <- filter(sg, PLOT1500$kndvi2500)
skndvi2500
sndvi2500 <- filter(sg, PLOT1500$ndvi2500)
sndvi2500
snirv2500 <- filter(sg, PLOT1500$nirv2500)
snirv2500
cc=cbind(sevi1500,sevi21500,skndvi1500,sndvi1500,snirv1500,sevi2500,sevi22500,skndvi2500,sndvi2500,snirv2500)
cc
Thanks in advance.
CodePudding user response:
library(tidyverse)
library(signal)
#>
#> Attaching package: 'signal'
#> The following object is masked from 'package:dplyr':
#>
#> filter
#> The following objects are masked from 'package:stats':
#>
#> filter, poly
PLOT1500 <- structure(list(
system = c(
"1-Jan-16", "2-Jan-16", "3-Jan-16",
"4-Jan-16"
), evi1500 = c(0.437, 0.3891771, 0.493, 0.261), evi21500 = c(
0.467436348,
0.186, 0.446531, 0.386620096
), kndvi1500 = c(
0.686, 0.413083514,
0.452461736, 0.508
), ndvi1500 = c(0.482, 0.57, 0.461, 0.198),
nirv1500 = c(0.453765489, 0.624, 0.694, 0.457), evi2500 = c(
0.439085339,
0.415, 0.447462363, 0.683
), evi22500 = c(
0.391, 0.715, 0.394061226,
0.382846651
), kndvi2500 = c(
0.452913701, 0.414950345, 0.420522988,
0.475
), ndvi2500 = c(0.32139863, 0.515688255, 0.427, 0.427274026), nirv2500 = c(0.491, 0.173, 0.444, 0.471)
), row.names = c(
NA,
4L
), class = "data.frame")
# need more rows for filter
PLOT1500 <- PLOT1500 %>%
list() %>%
rep(10) %>%
bind_rows()
sg <- sgolay(p = 7, n = 11, m = 0)
PLOT1500 %>%
mutate(across(matches("1500|2500"), ~ signal::filter(sg, .x)))
#> system evi1500 evi21500 kndvi1500 ndvi1500 nirv1500 evi2500
#> 1 1-Jan-16 0.4317215 0.4655495 0.6840037 0.4747651 0.4510836 0.4454103
#> 2 2-Jan-16 0.4231690 0.1996236 0.4273279 0.6157175 0.6402125 0.3747353
#> 3 3-Jan-16 0.4056273 0.4036501 0.4091449 0.3485501 0.6573740 0.5481791
#> 4 4-Jan-16 0.3696906 0.4656374 0.5812905 0.3199554 0.4881796 0.5678016
#> 5 1-Jan-16 0.3757619 0.3668323 0.6085295 0.4559650 0.4623747 0.4794025
#> 6 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 7 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 8 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 9 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 10 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 11 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 12 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 13 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 14 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 15 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 16 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 17 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 18 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 19 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 20 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 21 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 22 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 23 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 24 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 25 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 26 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 27 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 28 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 29 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 30 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 31 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 32 4-Jan-16 0.3391415 0.4079327 0.5283815 0.3073739 0.5039268 0.5884699
#> 33 1-Jan-16 0.3970425 0.4001966 0.5975225 0.4453961 0.4912674 0.4797898
#> 34 2-Jan-16 0.4141707 0.2904986 0.4728216 0.5251263 0.6016812 0.4315946
#> 35 3-Jan-16 0.4298224 0.3879595 0.4608195 0.4331037 0.6318901 0.4846934
#> 36 4-Jan-16 0.3451485 0.4596679 0.5446130 0.2676971 0.4990481 0.6103875
#> 37 1-Jan-16 0.3450376 0.3682986 0.5828770 0.3919248 0.4595737 0.5307030
#> 38 2-Jan-16 0.4498211 0.2610299 0.5040675 0.6315108 0.6015259 0.3519612
#> 39 3-Jan-16 0.4715542 0.4178171 0.4158430 0.4391927 0.7055284 0.4700416
#> 40 4-Jan-16 0.2641572 0.3910584 0.5137794 0.2011921 0.4549914 0.6796653
#> evi22500 kndvi2500 ndvi2500 nirv2500
#> 1 0.3907501 0.4538682 0.3229697 0.4910148
#> 2 0.7147841 0.4090610 0.5045815 0.1746777
#> 3 0.4045248 0.4343358 0.4605200 0.4302151
#> 4 0.3368979 0.4619705 0.3704317 0.5191066
#> 5 0.4902738 0.4516159 0.3837821 0.3961985
#> 6 0.5884952 0.4243567 0.4615026 0.2884093
#> 7 0.4510686 0.4302815 0.4409619 0.4001168
#> 8 0.3940673 0.4595071 0.4097488 0.4628453
#> 9 0.4492767 0.4492416 0.3791475 0.4276285
#> 10 0.5884952 0.4243567 0.4615026 0.2884093
#> 11 0.4510686 0.4302815 0.4409619 0.4001168
#> 12 0.3940673 0.4595071 0.4097488 0.4628453
#> 13 0.4492767 0.4492416 0.3791475 0.4276285
#> 14 0.5884952 0.4243567 0.4615026 0.2884093
#> 15 0.4510686 0.4302815 0.4409619 0.4001168
#> 16 0.3940673 0.4595071 0.4097488 0.4628453
#> 17 0.4492767 0.4492416 0.3791475 0.4276285
#> 18 0.5884952 0.4243567 0.4615026 0.2884093
#> 19 0.4510686 0.4302815 0.4409619 0.4001168
#> 20 0.3940673 0.4595071 0.4097488 0.4628453
#> 21 0.4492767 0.4492416 0.3791475 0.4276285
#> 22 0.5884952 0.4243567 0.4615026 0.2884093
#> 23 0.4510686 0.4302815 0.4409619 0.4001168
#> 24 0.3940673 0.4595071 0.4097488 0.4628453
#> 25 0.4492767 0.4492416 0.3791475 0.4276285
#> 26 0.5884952 0.4243567 0.4615026 0.2884093
#> 27 0.4510686 0.4302815 0.4409619 0.4001168
#> 28 0.3940673 0.4595071 0.4097488 0.4628453
#> 29 0.4492767 0.4492416 0.3791475 0.4276285
#> 30 0.5884952 0.4243567 0.4615026 0.2884093
#> 31 0.4510686 0.4302815 0.4409619 0.4001168
#> 32 0.3940673 0.4595071 0.4097488 0.4628453
#> 33 0.4492767 0.4492416 0.3791475 0.4276285
#> 34 0.5884952 0.4243567 0.4615026 0.2884093
#> 35 0.4510686 0.4302815 0.4409619 0.4001168
#> 36 0.3252712 0.4647988 0.3890248 0.5228858
#> 37 0.4673952 0.4575471 0.3958628 0.4115581
#> 38 0.6558883 0.4145530 0.4540116 0.2368532
#> 39 0.4171119 0.4201153 0.4513231 0.4188209
#> 40 0.3792356 0.4751102 0.4234683 0.4749589
Created on 2021-12-09 by the reprex package (v2.0.1)