Home > Enterprise >  "Error in xj[i] : only 0's may be mixed with negative subscripts" when I add a 0 row
"Error in xj[i] : only 0's may be mixed with negative subscripts" when I add a 0 row

Time:06-05

I have this df

dx <- structure(list(a = c(0.916290731874155, 2.89037175789616, -0.156004248476581, 
-0.318453731118534, -2.07944154167984, 2.00533356952611, -1.24319351747922, 
0.42744401482694, 1.29532258291416, -2.03292152604494, -0.606135803570316, 
-0.693147180559945), b = c(0.550046336919272, 0.228258651980981, 
-0.577634293438101, 0.135801541159061, 0.644357016390513, -2.30258509299405, 
-0.0870113769896297, 1.71297859137494, 0.17958557697508, -1.65140211153313, 
1.31218638896617, 0.282862786015832), c = c(0.0988458346366325, 
-3.34403896782221, 1.99243016469021, -1.70474809223843, 2.62103882411258, 
2.20727491318972, -1.40242374304977, -1.256836293883, -2.16905370036952, 
2.91777073208428, 0.138586163286146, -0.946143695023836), d = c(0.268263986594679, 
-2.83321334405622, 1.83258146374831, 1.15057202759882, 0.0613689463762919, 
-2.23359222150709, 4.34236137828145, -3.44854350225935, 1.29098418131557, 
-0.356674943938732, -0.21868920096483, -0.810930216216329), e = c(1.65140211153313, 
0.220400065368459, -0.044951387862266, 0.0773866636154201, -1.49877234454658, 
1.36219680954083, -0.295845383090942, -0.709676482511156, -0.916290731874155, 
1.65822807660353, 0.451985123743057, -0.810930216216329)), class = "data.frame", row.names = 2:13)

and i need to add a 0 row to df in a loop, because I need the 0 row as first row of each sequence of rows (1:4, 2:5, 3:6, etc).

rs <- 4
sr <- 2
for (t in (rs 1:12-sr)){
z <- 0
R <- Map(` `, list(t-rs:t-1), 0:z)
for (r in seq(R)) {
    s_df<- rbind(0,dx[R[[r]],])
  }
}

but returns me this error: Error in xj[i] : only 0's may be mixed with negative subscripts

The full loop I'm trying to get to work is

rs <- 4
sr <- 2
for (t in (rs 1:12-sr)){
z <- 0
R <- Map(` `, list(t-rs:t-1), 0:z)
cmin <- t(as.matrix(rep(NA, ncol(dx))))
cdf_mat <- matrix(NA, length(R), ncol(dx))
sq <- list()
for (r in seq(R)) {
  for (f in seq(ncol(dx))) {
    s_df<- rbind(0,dx[R[[r]],])
    df_cum <- sapply(out, function(x) ((cumsum(x))   1))
    x <- df_cum[,f]
    y <- df_cum[,-f]
    dif_2 <- (x - y)^2
    cmin[f] <- which.min(colSums(dif_2))
    dif_3 <- as.matrix(dif_2[,cmin[f]])
    cdf_mat[r,f] <- 
      if (f <= cmin[f]) {
        cmin[f]   1
      } else { 
        cmin[f]
      }
    sq <- c(sq, list(sqrt(dif_3)))
    sqmat <- do.call(cbind, sq)
    sd <- (colSums(sqmat))/t
  }
 }
}

Can you tell me why and how can I do?

Thank you

CodePudding user response:

What about this

insert_z <- function(df){
  i <- 1L
  ldf <- list()
  z <- rep(0 , ncol(df))
  while(i 2 < nrow(df)){
    df1 <- df[i:(i 3) , ]
    ldf[[i]] <- rbind(z , df1)
    i <- i   1L
  }
  do.call(rbind , ldf)
}

insert_z(dx)
#>              a           b           c           d           e
#> 1    0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 2    0.9162907  0.55004634  0.09884583  0.26826399  1.65140211
#> 3    2.8903718  0.22825865 -3.34403897 -2.83321334  0.22040007
#> 4   -0.1560042 -0.57763429  1.99243016  1.83258146 -0.04495139
#> 5   -0.3184537  0.13580154 -1.70474809  1.15057203  0.07738666
#> 14   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 31   2.8903718  0.22825865 -3.34403897 -2.83321334  0.22040007
#> 41  -0.1560042 -0.57763429  1.99243016  1.83258146 -0.04495139
#> 51  -0.3184537  0.13580154 -1.70474809  1.15057203  0.07738666
#> 6   -2.0794415  0.64435702  2.62103882  0.06136895 -1.49877234
#> 15   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 42  -0.1560042 -0.57763429  1.99243016  1.83258146 -0.04495139
#> 52  -0.3184537  0.13580154 -1.70474809  1.15057203  0.07738666
#> 61  -2.0794415  0.64435702  2.62103882  0.06136895 -1.49877234
#> 7    2.0053336 -2.30258509  2.20727491 -2.23359222  1.36219681
#> 16   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 53  -0.3184537  0.13580154 -1.70474809  1.15057203  0.07738666
#> 62  -2.0794415  0.64435702  2.62103882  0.06136895 -1.49877234
#> 71   2.0053336 -2.30258509  2.20727491 -2.23359222  1.36219681
#> 8   -1.2431935 -0.08701138 -1.40242374  4.34236138 -0.29584538
#> 17   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 63  -2.0794415  0.64435702  2.62103882  0.06136895 -1.49877234
#> 72   2.0053336 -2.30258509  2.20727491 -2.23359222  1.36219681
#> 81  -1.2431935 -0.08701138 -1.40242374  4.34236138 -0.29584538
#> 9    0.4274440  1.71297859 -1.25683629 -3.44854350 -0.70967648
#> 18   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 73   2.0053336 -2.30258509  2.20727491 -2.23359222  1.36219681
#> 82  -1.2431935 -0.08701138 -1.40242374  4.34236138 -0.29584538
#> 91   0.4274440  1.71297859 -1.25683629 -3.44854350 -0.70967648
#> 10   1.2953226  0.17958558 -2.16905370  1.29098418 -0.91629073
#> 19   0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 83  -1.2431935 -0.08701138 -1.40242374  4.34236138 -0.29584538
#> 92   0.4274440  1.71297859 -1.25683629 -3.44854350 -0.70967648
#> 101  1.2953226  0.17958558 -2.16905370  1.29098418 -0.91629073
#> 11  -2.0329215 -1.65140211  2.91777073 -0.35667494  1.65822808
#> 110  0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 93   0.4274440  1.71297859 -1.25683629 -3.44854350 -0.70967648
#> 102  1.2953226  0.17958558 -2.16905370  1.29098418 -0.91629073
#> 111 -2.0329215 -1.65140211  2.91777073 -0.35667494  1.65822808
#> 12  -0.6061358  1.31218639  0.13858616 -0.21868920  0.45198512
#> 112  0.0000000  0.00000000  0.00000000  0.00000000  0.00000000
#> 103  1.2953226  0.17958558 -2.16905370  1.29098418 -0.91629073
#> 113 -2.0329215 -1.65140211  2.91777073 -0.35667494  1.65822808
#> 121 -0.6061358  1.31218639  0.13858616 -0.21868920  0.45198512
#> 13  -0.6931472  0.28286279 -0.94614370 -0.81093022 -0.81093022

Created on 2022-06-04 by the reprex package (v2.0.1)

CodePudding user response:

We may do this easily with slider

library(slider)
out <- lapply(Filter(\(x) length(x) == rs, slide(seq_len(nrow(dx)), 
    .after = rs-1, .f = I)), \(x) rbind(0, dx[x,]))

-output

> head(out, 3)
[[1]]
           a          b           c         d           e
1  0.0000000  0.0000000  0.00000000  0.000000  0.00000000
2  0.9162907  0.5500463  0.09884583  0.268264  1.65140211
3  2.8903718  0.2282587 -3.34403897 -2.833213  0.22040007
4 -0.1560042 -0.5776343  1.99243016  1.832581 -0.04495139
5 -0.3184537  0.1358015 -1.70474809  1.150572  0.07738666

[[2]]
           a          b         c           d           e
1  0.0000000  0.0000000  0.000000  0.00000000  0.00000000
3  2.8903718  0.2282587 -3.344039 -2.83321334  0.22040007
4 -0.1560042 -0.5776343  1.992430  1.83258146 -0.04495139
5 -0.3184537  0.1358015 -1.704748  1.15057203  0.07738666
6 -2.0794415  0.6443570  2.621039  0.06136895 -1.49877234

[[3]]
           a          b         c           d           e
1  0.0000000  0.0000000  0.000000  0.00000000  0.00000000
4 -0.1560042 -0.5776343  1.992430  1.83258146 -0.04495139
5 -0.3184537  0.1358015 -1.704748  1.15057203  0.07738666
6 -2.0794415  0.6443570  2.621039  0.06136895 -1.49877234
7  2.0053336 -2.3025851  2.207275 -2.23359222  1.36219681

If we want a single dataset, rbind the list elements

out2 <- do.call(rbind, out)
  • Related