How to extract date time information from timestamps in R

r
datetime

#1

Hello,

I have a column which is a time stamp and I intend to extract the year,month and day from this.
I came across the below python code for this:

#timestamp_first_active
tfa = np.vstack(df_all.timestamp_first_active.astype(str).apply(lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]]))).values)
df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1)

How can I achieve something like this in R?

I have tried:

#timestamp_first_active
tfa = as.POSIXct(as.numeric(as.character(df_all$timestamp_first_active)),origin="1970-01-01",tz="GMT")
df_all['year_first'] <- year(tfa) 

But this gives:

As can be seen the dates are not proper.
Can someone please help me with this??


#2

Hey for extracting day year or month.Try this