S*********1 发帖数: 3004 | 1 请问:有"出生日期"和"测试日期",格式:mo/day/year,怎么算测试时的年龄
,我觉得可以用if,ifelse之类的,但是写不出来
dob doa
1/2/2001 1/3/2001
9/20/2001 9/20/2011
12/14/2000 11/25/2010
谁能给个例子,包子酬谢 | l*********s 发帖数: 5409 | | S*********1 发帖数: 3004 | 3
我其实是想,能不能用年相减,然后条件是出生月份和日子都要比测试那天大,否则减
去一岁。但是写不好。
【在 l*********s 的大作中提到】 : read this : http://www.statmethods.net/input/dates.html
| l*********s 发帖数: 5409 | 4 say, d <- "12/10/2001",
datastruct <- as.numeric( unlist( strsplit(d, "/")) )
datastruct is a tuple of (month, day,year). You shall be able to figure out
the rest stuff on your own now.
baozi plz. | S*********1 发帖数: 3004 | 5
out
接包子吧,偶们学社科的,穷得只剩下包子了~
【在 l*********s 的大作中提到】 : say, d <- "12/10/2001", : datastruct <- as.numeric( unlist( strsplit(d, "/")) ) : datastruct is a tuple of (month, day,year). You shall be able to figure out : the rest stuff on your own now. : baozi plz.
| l*********s 发帖数: 5409 | 6 Social Science is not poor; biology is poorer :-)
【在 S*********1 的大作中提到】 : : out : 接包子吧,偶们学社科的,穷得只剩下包子了~
| a****u 发帖数: 95 | 7 doa <- as.Date(doa, "%m/%d/%Y")
dob <- as.Date(dob, "%m/%d/%Y")
age.days<-as.numeric(difftime(dob,doa),units="days")
or
age.days<-as.numeric(dob-doa) | S*********1 发帖数: 3004 | 8
I want "age", even I could calculate the days, not 365 days every year, some examples use "days/365.25", won't give a good result
【在 a****u 的大作中提到】 : doa <- as.Date(doa, "%m/%d/%Y") : dob <- as.Date(dob, "%m/%d/%Y") : age.days<-as.numeric(difftime(dob,doa),units="days") : or : age.days<-as.numeric(dob-doa)
| S*********1 发帖数: 3004 | 9
both poor
【在 l*********s 的大作中提到】 : Social Science is not poor; biology is poorer :-)
| s*****n 发帖数: 2174 | 10 temp <-
as.numeric(unlist(strsplit(format(as.Date(doa, "%m/%d/%Y"), "%Y-%m%d"),
split = "-"))) -
as.numeric(unlist(strsplit(format(as.Date(dob, "%m/%d/%Y"), "%Y-%m%d"),
split = "-")))
temp[1] - as.numeric(temp[2] < 0)
【在 S*********1 的大作中提到】 : : both poor
| g**a 发帖数: 2129 | 11 x<-as.POSIXlt(strptime(dob,"%m/%d/%Y"))
y<-as.POSIXlt(strptime(doa,"%m/%d/%Y"))
age<-y$year-x$year |
|