Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

How to extract date time information from timestamps in R

$
0
0

@hackers wrote:

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??

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles