本文介绍了将日期数据从SAS转入R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问这个问题,我将一个sas7数据集应用到R中是非常奇怪的。
我的一个变量是visit_date

It's quite weird to ask this question that I apply a sas7 dataset into R.One of my variable is visit_date

现在看起来像这样,我想知道我可以将其转换回MM-DD-YYYY,因为我需要排除小于MDY的数据(08-01-2010)。

now it looks like this, i am wondering where i can transform them back to MM-DD-YYYY since i need to exclude data that's less than MDY(08-01-2010).

> chris$visit_date
  [1] 17077 17091 17105 17119 17133 17069 17083 17097 17111 17125 17080 17094 17108
 [14] 17122 17136 17098 17112 17210 17224 17238 17252 17266 17247 17261 17254 17268
 [27] 17282 17296 17324 17237 17251 17265 17279 17293 17329 17343 17357 17385 17413
 [40] 17259 17273 17287 17301 17315 17328 17342 17356 17370 17384 17335 17349 17377
 [53] 17391 17405 17331 17345 17359 17373 17387 17435 17449 17463 17477 17505 17336
 [66] 17364 17378 17392 17406 17352 17366 17380 17394 17408 17427 17441 17469 17483
 [79] 17497 17440 17454 17468 17482 17496 17434 17448 17462 17476 17490 17419 17433
 [92] 17447 17461 17475 17518 17560 17574 17588 17616 17653 17667 17681 17695 17709
[105] 17644 17658 17686 17700 17728 17755 17769 17783 17811 17825 17825 17610 17624
[118] 17638 17652 17666 18072 18114 18127 18155 18169 17651 17665 17680 17693 17707
[131] 17657 17671 17685 17699 17659 17673 17687 17701 17715 17646 17660 17674 17688
[144] 17702 17721 17735 17749 17763 17770 17734 17748 17762 17790 17861 17736 17750
[157] 17764 17778 17792 17751 17765 17779 17793 17807 17742 17756 17770 17784 17798
[170] 17772 17757 17771 17785 17799 17813 17777 17791 17819 17833 17854 17923 17937
[183] 17965 17979 17993 17825 17839 17853 17867 17909 17832 17846 17860 17874 17888
[196] 17919 17933 17961 17975 17989 17960 17974 17988 18002 18016 18183 18211 18225
[209] 18239 18253 17931 17945 17959 17973 17987 17940 17954 17968 17982 17996 17966
[222] 17980 17994 18022 18036 18021 18035 18049 18063 18091 18050 18064 18078 18092
[235] 18106 18045 18059 18073 18087 18115 18024 18038 18052 18066 18080 18056 18070
[248] 18084 18098 18112 18107 18121 18135 18149 18163 18105 18119 18133 18161 18175
[261] 18143 18171 18185 18199 18213 18203 18246 18274 18288 18302 18316 18248 18276
[274] 18290 18304 18318 18310 18324 18338 18352 18366 18315 18343 18357 18364 18378
[287] 18350 18364 18378 18406 18420 18337 18351 18365 18379 18393 18374 18388 18402
[300] 18430 18472 18344 18358 18386 18400 18414 18353 18381 18395 18409 18423 18387
[313] 18415 18429 18443 18450 18408 18422 18436 18443 18464 18430 18437 18457 18464
[326] 18471 18427 18434 18441 18455 18462 18428 18442 18456 18463 18470

谢谢

推荐答案

这些日期明显地使用与可以用于此转换的典型POSIX标准不同的起始/偏移量。 R通常使用YYYY-MM-DD格式

Those "dates" are clearly using a different origin/offset than the typical POSIX standard that would work with this conversion. R generally uses YYYY-MM-DD format

as.Date(ddd, origin="1970-01-01")

> head( as.Date(ddd, origin="1970-01-01") )
[1] "2016-10-03" "2016-10-17" "2016-10-31" "2016-11-14" "2016-11-28" "2016-09-25"

所以你需要建立正确的起源。如果是1960-01-01,那么这些日期都不会大于08-01-2010。

So you need to establish the correct origin. If it was 1960-01-01, then none of those dates is greater than 08-01-2010.

> sum( as.Date(ddd, origin="1960-01-01") >= as.Date("2010-08-01") )
[1] 0
> sum( as.Date(ddd, origin="1960-01-01") < as.Date("2010-08-01") )
[1] 336

这篇关于将日期数据从SAS转入R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:13