0

I'm trying to gather three data frames together that has a date column. I am using rbind() and I get this error:

character string is not an ambiguous form

In the second df, the date column is as character type. The others are as POSIXlt. I know this is the problem, but don't know how to solve it. Could someone help?

Also, do you have any tutorial that teaches what is POSIXlt and what does it do?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

POSIXlt and POSIXct are the two built-in data types for date-times in R. This Q&A has more explanation.

Under the hood, both describe the amount of time that has passed since the first moment of 1970.

Here I define two POSIXlt values. I am in PST time zone, 8 hours behind GMT, so the first date-time I picked is actually the same moment as 1970-01-01 00:00 GMT

my_times <- as.POSIXlt(c("1969-12-31 16:00", "2021-04-15 13:49"))
my_times
#[1] "1969-12-31 16:00:00 PST" "2021-04-15 13:49:00 PDT"

Under the hood, each timestamp in POSIXlt format is a list of numbers each describing the year, date, hour, etc., with some extra flags to tell R that its a POSIXlt, my timezone and whether its daylight savings time, etc.

# dput creates code that would reproduce those values. You'll see that
#   it encodes them by coding the year, month, day, hour, etc.
dput(my_times)
#structure(list(sec = c(0, 0), min = c(0L, 49L), hour = c(16L, 
#13L), mday = c(31L, 15L), mon = c(11L, 3L), year = c(69L, 121L
#), wday = 3:4, yday = c(364L, 104L), isdst = 0:1, zone = c("PST", 
#"PDT"), gmtoff = c(NA_integer_, NA_integer_)), class = c("POSIXlt", 
#"POSIXt"))

# FYI, POSIXct stores the numbers directly as seconds since 1970 
#   (equivalent to the way POSIXct stores them)
structure(c(0, 1618519740), class = c("POSIXct", "POSIXt"), tzone = "")

If we need to convert those two timestamps each to a single number, R will convert it to the number of seconds since the start of 1970. The first one was picked to be that moment (0 seconds elapsed), while about 1.6 billion seconds have now elapsed.

as.numeric(my_times)
#[1]          0 1618519740

# approx years since start of 1970
# calculated by looking at the difference between the two numbers
diff(as.numeric(my_times))/(24*60*60*365.25)
#[1] 51.2878

To convert to character, you can use as.character to get them like this:

as.character(my_times)
#[1] "1969-12-31 16:00:00" "2021-04-15 13:49:00"

If you need them in a specific time format, see here for a review of those using the strptime function:

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

install lubridate package for this.

The arrays wont bind if they have different data types. convert all the three Date columns to same type :

df1$DATE = as.Date(df1$DATE ,"%Y-%m-%d")

if the dates are in form "2021-04-16" ;; use "%m-%d-%Y" for 04-16-2021 and "%d-%b-%Y" for 04-Apr-2021. Replace "-" with "/" if the dates are in form "04/16/2021" then , Similarly ,

df2$DATE = as.Date(df2$DATE ,"%Y-%m-%d")

df3$DATE = as.Date(df2$DATE ,"%Y-%m-%d")

then ,call

df_final = rbind(df1 ,df2 ,df3)

WilliamGram
  • 673
  • 3
  • 7
pppp_prs
  • 106
  • 1
  • 8