1

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.

hlyates
  • 1,279
  • 3
  • 22
  • 44
  • Please try to give a [reproducible example](https://stackoverflow.com/questions/14976331/use-of-tilde-in-r-programming-language). Share data in a way that can be copy/pasted into R. I can't recreate the error from your example currently. – MrFlick May 28 '17 at 14:40
  • 2
    `steps` is a dataframe in a dataframe. Is this as intended? Moreover, your `row` does not mirror that format. – KoenV May 28 '17 at 14:42
  • @KoenV No it is certainly not intended. I missed that detail. I want df to be a numeric column...not a dataframe. I'll include my loop code. – hlyates May 28 '17 at 14:47
  • @MrFlick The data I am using is coming from a csv, so it's not easy to do. That said, please do not downvote me and I'll see what I can do to better the problem. In the future, I will make sure all my problems will be reproducible. – hlyates May 28 '17 at 14:48
  • @KoenV Okay I updated the problem definition will all the details. Please be so kind and gracious to let me know if you see any obvious problem. I'm looking into why I'm getting the weird nested dataframe problem. – hlyates May 28 '17 at 14:59
  • I know why. I should cast my `subset` to numeric. Based on the docs, `subset` is returning a dataframe and casting my steps to a df I think? If you want, you can post that as the answer and I'll accept it or I can post it myself. Let me know what you want to do. I hope this helps others with this kind of problem. Tricky! – hlyates May 28 '17 at 15:01
  • 1
    You found the solution, you post it ;-) – KoenV May 28 '17 at 15:28
  • @KoenV Thanks for your comment and help. You got me unstuck and I really needed the second pair of eyes. – hlyates May 28 '17 at 17:10
  • My pleasure. Glad I could help. – KoenV May 28 '17 at 20:21

1 Answers1

0

Simple Answer

Do the following: row$steps <- subset(avgStepsByInterval, interval == row$interval, steps)

Explanation

The problem was that steps was a dataframe object. Said in another way, we were trying to bind a row that had the desired dataframe definition to a dataframe object df that didn't have the proper definitions.

This is because row$steps <- subset(avgStepsByInterval, interval == row$interval, steps) is returning a dataframe object. So we simply need to rewrite it as follows:

row$steps <- as.numeric(subset(avgStepsByInterval, interval == row$interval, steps))

This means the definition for df and row will match.

Summary

If you are searching on stackoverflow and you are seeing this error. Be sure that the objects you are trying to rbind have a matching and intended structure.

hlyates
  • 1,279
  • 3
  • 22
  • 44