Problem
I am doing a rbind and I am experiencing the following error as shown below:
Error in rbind(deparse.level, ...) : replacement has length zero
How may I resolve this error?
Details
The code is inside a loop. I am going through a dataframe and if a certain row has a null value for steps, I replace it with something and move on. That was working great until I got to row 289 which basically as far as I know looks exactly just like those before it (except the interval is 0 instead of 2355, but when I changed it to 2356 just to check if that was it, still get the same error, so this is some sort of formatting thing).
The code is as follows:
rbind(df, row)
I have a data frame, lets call it df for short that looks like something like this.
steps interval dates
283 2.6037736 2330 2012-10-01
284 4.6981132 2335 2012-10-01
285 3.3018868 2340 2012-10-01
286 0.6415094 2345 2012-10-01
287 0.2264151 2350 2012-10-01
288 1.0754717 2355 2012-10-01
The row is as follows:
steps interval dates
289 0 0 2012-10-02
Here are some details about df and row as follows:
data.frame': 288 obs. of 3 variables:
$ steps :'data.frame': 288 obs. of 1 variable:
..$ steps: num 1.717 0.3396 0.1321 0.1509 0.0755 ...
$ interval: int 0 5 10 15 20 25 30 35 40 45 ...
$ dates : Date, format: "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01" ...
and
'data.frame': 1 obs. of 3 variables:
$ steps : int 0
$ interval: int 0
$ dates : Date, format: "2012-10-02"
Update
I think the problem is that my steps is a dataframe within a dataframe. It should be just a column of numeric values. Here is some code that specifies the intended definition for df
. Please see:
df <- data.frame(steps = numeric, date = date, interval = integer)
> str(df)
'data.frame': 0 obs. of 3 variables:
$ steps :function (...)
$ date :function (...)
$ interval:function (...)
The following cannot be easily duplicated, I apologize in advanced, but I can describe what the code is doing. I have a dataset that has NA
in steps
and I use row$steps <- subset(avgStepsByInterval, interval == row$interval, steps)
to replace that NA
with a number. For example, the idea is that:
steps interval dates
289 NA 0 2012-10-02
Becomes
steps interval dates
289 0 0 2012-10-02
The loop I am using is as follows:
for(i in 1:size)
{
index<-i
row <- data[i,]
isNa <- is.na(row$steps)
if(isNa)
{
row$steps <- subset(avgStepsByInterval, interval == row$interval, steps)
}
df<-rbind(df, row)
}
Don't worry about trying to duplicate the above code, I don't think that is important, but I am hoping it can help clue folks into something obviously wrong that I might be doing. If not, let me know and I'll see if I can create some replicable code (which will take some time). It might be blowing up because steps
is somehow a dataframe within a dataframe in df
. Which is not right. Our row
definition is correct.
Attempts
I have tried the following approaches to no avail:
- Format row with a
do.call
method - Cast row to data.frame (which it already is)
- Manipulating values in steps and interval in row.
I'm thinking there is obviously something wrong with my formatting but I can't fix a problem if I don't know what is wrong. The above is all I was able to gather from debugging the code. I'm frankly completely at a loss and would appreciate someone with more experience than me to indicate to me what is going wrong and how I can best address it? Any assistance, questions, or comments would be most welcome.