I have a dataframe df that I want to add a column to called row_num that represents the index of the row. This was my initial solution:
df$row_num<-seq(1:nrow(df))
However, it doesn't work in the case that df is empty as I get the error:
Error in `$<-.data.frame`(`*tmp*`, row_num, value = 1:2) :
replacement has 2 rows, data has 0
One solution I have found is using row_number() from dplyr but this seems to slow down my code quite a bit so I was looking for a simpler solution.
CodePudding user response:
Two problems here:
1:nrow(df)
will always return something, even ifnrow(df)
is 0. Try it!1:0 # [1] 1 0
This is because
1:0
is seen as a reverse sequence, so it counts down.seq(1:nrow(df))
is redundant. Fortunately, this isn't breaking your code, it's just sequencing along it, but it's not helping.seq(1:9) # [1] 1 2 3 4 5 6 7 8 9 seq(1:0) # [1] 1 2
One fix is to just use seq_len(nrow(df))
, where seq_len(0)
(for a 0-row frame) will return the length vector you need (i.e., length 0). (In general, I tend to recommend seq_len(..)
any time unsupervised code is meant to iterate a programmatic number of times. Another "safe" function is seq_along(..)
, as it will also do nothing if its input is length 0, but it isn't useful in this case.)