DS4PS / ddmp-uw-class-spring-2019

Data-Driven Management & Policy Course at UW, Spring 2019
https://ds4ps.github.io/ddmp-uw-class-spring-2019/
4 stars 3 forks source link

Problem of calculating the age #5

Open boyaow opened 5 years ago

boyaow commented 5 years ago

I wrote the code to calculate the age of a person, but R shows me NA, what is the problem? I created the list and the list showed nothing wrong. 屏幕快照 2019-04-20 下午3 50 57 屏幕快照 2019-04-20 下午3 51 05 屏幕快照 2019-04-20 下午3 51 12

lecy commented 5 years ago

It looks fine. Did you run the chunk twice and convert your DateOfBirth twice?

> dob <- "9 February 1951"
> dob.as.date <- as.Date( dob, format="%d %B %Y" )
> dob.as.date
[1] "1951-02-09"
> Sys.Date()
[1] "2019-04-20"
> time.diff <- Sys.Date() - dob.as.date  # in days
> time.diff
Time difference of 24907 days
> as.numeric( time.diff ) / 365  # in years
[1] 68.23836
MAGALLANESJoseManuel commented 5 years ago

Just i n case, use:

Sys.setlocale("LC_TIME", "English")

Before making the date computation. Your computer may have a setting different than English.

Please, let us know it is working well.

boyaow commented 5 years ago

I put this chunk of code before Sys.Date(), but it shows something else. 屏幕快照 2019-04-21 上午11 06 21

lecy commented 5 years ago

It's giving you an error when you are trying to set the locale time. Can you type sessionInfo() and share the results?

It's better if you share your code directly as text instead of a screen shot because it's easier for us to try the code to see where it might be broken.

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tigris_0.7      geojsonio_0.6.0 sp_1.3-1  
boyaow commented 5 years ago

I tried this code and it shows something below, actually I think that kind of problem does not show up after I changed my system language of the computer to English. R version 3.5.2 (2018-12-20) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.3

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] lubridate_1.7.4

loaded via a namespace (and not attached): [1] compiler_3.5.2 magrittr_1.5 rsconnect_0.8.13 htmltools_0.3.6 [5] tools_3.5.2 yaml_2.2.0 Rcpp_1.0.0 rmarkdown_1.11
[9] stringi_1.2.4 knitr_1.21 digest_0.6.18 stringr_1.3.1
[13] xfun_0.4 evaluate_0.12

boyaow commented 5 years ago

I can now run these code without the code Sys.setlocale("LC_TIME", "English")