Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem of calculating the age #5

Open
boyaow opened this issue Apr 20, 2019 · 6 comments
Open

Problem of calculating the age #5

boyaow opened this issue Apr 20, 2019 · 6 comments

Comments

@boyaow
Copy link

boyaow commented Apr 20, 2019

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

@boyaow boyaow changed the title Problem 藕粉 Problem of calculating the age Apr 20, 2019
@lecy
Copy link
Contributor

lecy commented Apr 20, 2019

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
Copy link
Collaborator

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
Copy link
Author

boyaow commented Apr 21, 2019

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

@lecy
Copy link
Contributor

lecy commented Apr 21, 2019

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
Copy link
Author

boyaow commented Apr 22, 2019

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
Copy link
Author

boyaow commented Apr 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants