Home > OS >  Referencing to a value in the next row in an if statement
Referencing to a value in the next row in an if statement

Time:05-17

I'm working with a dataframe of which i want to remove the first and the last row of a group. For some reason doing so with the first row works fine, but when I use the same code for the last row I get an error.

head(pilott0)
   Sensor0 Treatment Testnr RPM index waveperiod
1 890.2380      T1_S  T1_1S 780   298         NA
2 928.1808      T1_S  T1_1S 780   636        338
3 923.4647      T1_S  T1_1S 780   975        339
4 929.1885      T1_S  T1_1S 780  1313        338
5 885.0237      T1_S  T1_1S 780  1652        339
6 918.6435      T1_S  T1_1S 780  1988        336

I used the following code for changing the waveperiod of a new Testnr to NA, which worked.

for(i in 2:nrow(pilott0)){
  if(pilott0$Testnr[i] != pilott0$Testnr[i-1]){
    pilott0$waveperiod[i]=NA}}

While using the same method to change the last waveperiod of a Testnr gave an error

for(i in 2:nrow(pilott0)){
  if(pilott0$Testnr[i] != pilott0$Testnr[i 1]){
    pilott0$waveperiod[i]=NA}}

Error in if (pilott0$Testnr[i] != pilott0$Testnr[i   1]) { : 
  missing value where TRUE/FALSE needed

Since the first waveperiod value is a NA already i chose to use teh following code for removing the first and last row of each Testnr.

pilott0=subset(pilott0,pilott0$waveperiod != "NA")

CodePudding user response:

I have changed the last value of Testnr since in the question they were all equal. The base r code below avoids loops and I believe solves the problem.

pilott0 <- read.table(text = "
Sensor0 Treatment Testnr RPM index waveperiod
1 890.2380      T1_S  T1_1S 780   298         NA
2 928.1808      T1_S  T1_1S 780   636        338
3 923.4647      T1_S  T1_1S 780   975        339
4 929.1885      T1_S  T1_1S 780  1313        338
5 885.0237      T1_S  T1_1S 780  1652        339
6 918.6435      T1_S  T1_2S 780  1988        336
", header = TRUE)

flag <- c(pilott0$Testnr[-1], pilott0$Testnr[nrow(pilott0)]) != pilott0$Testnr
is.na(pilott0$waveperiod) <- flag

flag <- c(pilott0$Testnr[1], pilott0$Testnr[-nrow(pilott0)]) != pilott0$Testnr
is.na(pilott0$waveperiod) <- flag

pilott0
#>    Sensor0 Treatment Testnr RPM index waveperiod
#> 1 890.2380      T1_S  T1_1S 780   298         NA
#> 2 928.1808      T1_S  T1_1S 780   636        338
#> 3 923.4647      T1_S  T1_1S 780   975        339
#> 4 929.1885      T1_S  T1_1S 780  1313        338
#> 5 885.0237      T1_S  T1_1S 780  1652         NA
#> 6 918.6435      T1_S  T1_2S 780  1988         NA

Created on 2022-05-16 by the reprex package (v2.0.1)

CodePudding user response:

Consider ave with seq_along and length to remove first and last values by Testnr group.

pilott0 <- within(
    pilott0, {
        grp_num <- ave(1:nrow(pilott0), Testnr, FUN=seq_along)
        grp_cnt <- ave(1:nrow(pilott0), Testnr, FUN=length)

        waveperiod <- ifelse(grp_num %in% c(1, grp_cnt), NA_integer_, waveperiod)

        rm(grp_num, grp_cnt)
    }
)
  • Related