Python&R: 年齢の計算

I.Rで特定の日から現在までの年数

 

luburidateを使用します。

第二次世界大戦開始から現在までの年数を算出しましょう。years(1)の1は1倍という意味です。2であれば1/2になります。

 

library(lubridate)
worldWarIIbegins <- mdy(09021939)
today <- today()
interval(worldWarIIbegins,today)/years(1)
 

> interval(worldWarIIbegins,today)/years(1)
[1] 80.10383
 

 

II.Pythonで特定の日から現在までの年数

 

datetimeを使用します。次に年齢を計算する関数が紹介されていました。

 

Python program to calculate age in year

 

# calculate age in years 
from datetime import date
def calculateAge(birthDate):
    today = date.today()
    age = today.year - birthDate.year - \
          ((today.month, today.day) < (birthDate.month, birthDate.day))
    return age
# Driver code
print("Krint Eastwood :", calculateAge(date(1930, 5, 31)), "years")

 

次の結果が表示されます。

 

Krint Eastwood : 89 years 

Please follow and like us:
error

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.