-1

I am using this code:

df1 <- data.frame(
      x1 = 'a'
      , x2 = 'b'
      , y = 0
)


df1 <- do.call("rbind", replicate(10, df1, simplify = FALSE))

for (row in 1:nrow(df1)) {
    df1[row,]$y = (row - 1) 
}

df1

Can this be improved by using an apply family function instead of the loop? Thanks!

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 2
    Just `df1$y <- seq_along(df1$y) - 1` – utubun Feb 17 '19 at 17:28
  • 1
    Or `2:nrow(df1)`. – Roman Luštrik Feb 17 '19 at 17:43
  • ok thanks all - maybe write an answer? thanks. – cs0815 Feb 17 '19 at 17:51
  • 1
    BTW - `apply` functions *are* loops – dww Feb 17 '19 at 19:02
  • @dww - not sure what the point of your comment is ... but they are more efficient - hence my question ... – cs0815 Feb 17 '19 at 19:15
  • 2
    My point was that you seemed to be under the (common) misconception that apply is not a loop. Moreover, re. performance see, for example, ["Is R's apply family more than syntactic sugar?"](https://stackoverflow.com/a/2276001/2761575) and ["it is an already long debunked myth that for loops are any slower than lapply"](https://stackoverflow.com/a/42440872/2761575) – dww Feb 17 '19 at 19:26

1 Answers1

1

What about this solution?

df1 <- data.frame(
    x1 = 'a'
    , x2 = 'b'
    , y = 0
)


df1 <- do.call("rbind", replicate(10, df1, simplify = FALSE))


df1$y <- sapply(1:nrow(df1), function(i)as.numeric(row.names(df1))[i]-1)
Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21
  • No need for `sapply` (which is a hidden loop). A vectorized solution is available. See comment above by @utubun. – Parfait Feb 17 '19 at 20:48