how to add a calculated column into a data frame

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to add a calculated column into a data frame

Charles Thuo
Dear  Sirs,

I have a data frame that has a column that shows the transaction date.

How do i add another column that  extracts the year of transaction from the
transaction date.

Charles

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how to add a calculated column into a data frame

Jeff Newmiller
This should get you started:

x <- "2009-03-21"
substr( x, 1, 4 )
y <- as.integer( substr( x, 1, 4 ) )
y

or

yy <- as.POSIXlt( x )$year + 1900
yy

RShowDoc( "R-intro" )

On June 4, 2020 9:18:00 PM PDT, Charles Thuo <[hidden email]> wrote:

>Dear  Sirs,
>
>I have a data frame that has a column that shows the transaction date.
>
>How do i add another column that  extracts the year of transaction from
>the
>transaction date.
>
>Charles
>
> [[alternative HTML version deleted]]

Please post in plain text. It will get converted for you if you forget, and the quality of the automatic conversion can be awful.

>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how to add a calculated column into a data frame

Ravi Jeyaraman
In reply to this post by Charles Thuo
How about something like this?

df <- data.frame(ID=1:3, DTVAL=c("2009-03-21","2010-05-11","2020-05-05"))

df <- df %>% mutate(YEAR = as.numeric(format(as.Date(DTVAL,'%Y-%m-%d'),
'%Y')))



-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Charles Thuo
Sent: Friday, June 05, 2020 12:18 AM
To: [hidden email]
Subject: [R] how to add a calculated column into a data frame

Dear  Sirs,

I have a data frame that has a column that shows the transaction date.

How do i add another column that  extracts the year of transaction from the
transaction date.

Charles

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] <mailto:[hidden email]>  mailing list -- To
UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


--
This email has been checked for viruses by AVG.
https://www.avg.com

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.