user23451421
user23451421

Reputation: 1

Why does rbind change one value when appending to a data frame?

I am working with vectors of datetimes and time zones. A few of the time zones were recorded incorrectly and I want to adjust the times to fit the correct time zone. I want to output a data frame. It appears that when appending lines to the output data frame, rbind changes the value of one of the variables in one of the observations. Here is the an example code and the output showing the problem.

toy_problem <- function(){
  require("tidyverse")
   
  DateTime  <- c("2001-04-06 01:20:00",   # Input dates and times
                 "2001-04-05 19:20:00",
                 "2001-04-05 16:00:00",
                 "2001-04-06 01:15:00")
  listZones <- c("America/Chicago",       # Input time zones
                  "America/Chicago",
                  "America/Chicago",
                  "America/Chicago")
  trueZones  <- c("America/Detroit",      # Correct time zones
                  "America/Detroit",
                  "America/Chicago",
                  "America/Detroit")
  
  # Create an empty data frame
  
  outFrame   <- data.frame(inTime=NULL, outTime=NULL)
  
  for (i in 1:4) {                                        # Loop through the observations
   tReg        <- ymd_hms(DateTime[i], tz=listZones[i])   # Iinput date/time to POSIXct
   localTime   <- with_tz(tReg,tzone=trueZones[i])        # Fix the time to fit the correct TZ
   print(tReg)                                            # Starting date/time
   print(localTime)                                       # Adusted date/time adjusted
   
   # Put the observation into a one-line data frame
   
   thisFrame   <- data.frame(inTime=tReg, outTime=localTime)
   print(str(thisFrame))                                  # Check the one-line data frame
   outFrame    <- rbind(outFrame,thisFrame)               # Append to the ouput frame
  }
  print(outFrame)                                         # Print the output data frame
}

Here is the annotated output:

[1] "2001-04-06 01:20:00 CDT"
[1] "2001-04-06 02:20:00 EDT"                             # Correct
'data.frame':   1 obs. of  2 variables:                   # Looks good
 $ inTime : POSIXct, format: "2001-04-06 01:20:00"
 $ outTime: POSIXct, format: "2001-04-06 02:20:00"
NULL
[1] "2001-04-05 19:20:00 CDT"                             
[1] "2001-04-05 20:20:00 EDT"                             # Correct
'data.frame':   1 obs. of  2 variables:                   # Looks good
 $ inTime : POSIXct, format: "2001-04-05 19:20:00"
 $ outTime: POSIXct, format: "2001-04-05 20:20:00"
NULL
[1] "2001-04-05 16:00:00 CDT"                      
[1] "2001-04-05 16:00:00 CDT"                             # Correct!!!!
'data.frame':   1 obs. of  2 variables:                   # Looks good!!!
 $ inTime : POSIXct, format: "2001-04-05 16:00:00"
 $ outTime: POSIXct, format: "2001-04-05 16:00:00"
NULL
[1] "2001-04-06 01:15:00 CDT"
[1] "2001-04-06 02:15:00 EDT"                             # Correct
'data.frame':   1 obs. of  2 variables:                   # Looks good
 $ inTime : POSIXct, format: "2001-04-06 01:15:00"
 $ outTime: POSIXct, format: "2001-04-06 02:15:00"
NULL
               inTime             outTime
1 2001-04-06 01:20:00 2001-04-06 02:20:00                 # Correct
2 2001-04-05 19:20:00 2001-04-05 20:20:00                 # Correct
3 2001-04-05 16:00:00 2001-04-05 17:00:00                 # WRONG!!!!!!
4 2001-04-06 01:15:00 2001-04-06 02:15:00                 # Correct

I don't understand why that one value is being changed when it is appended to the output data frame.

Upvotes: 0

Views: 112

Answers (1)

user23451421
user23451421

Reputation: 1

Mike's comment was correct. rbind coerces classes so the class of the first entry was carried forward to all the others. The reason that one entry was changed because its class differed from the first one put into the output data frame.

Upvotes: 0

Related Questions