This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
geography<-'Geography Rocks'
print(n)
[1] "Geography Rocks"
# This is my very first R program!
Question 1
x=3
y=5
x+y
[1] 8
Question 2
x=55
class(x)
[1] "numeric"
first, we need to install packages
install.packages("leaflet")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
将程序包安装入‘C:/Users/r/AppData/Local/R/win-library/4.2’
(因为‘lib’没有被指定)
还安装相依关系‘leaflet.providers’
有二进制版本的,但源代码版本是后来的:
trying URL 'http://cran.rstudio.com/bin/windows/contrib/4.2/leaflet.providers_2.0.0.zip'
Content type 'application/zip' length 44599 bytes (43 KB)
downloaded 43 KB
程序包‘leaflet.providers’打开成功,MD5和检查也通过
下载的二进制程序包在
C:\Users\r\AppData\Local\Temp\RtmpO65oT9\downloaded_packages里
安装源码包‘leaflet’
trying URL 'http://cran.rstudio.com/src/contrib/leaflet_2.2.3.tar.gz'
Content type 'application/x-gzip' length 2013248 bytes (1.9 MB)
downloaded 1.9 MB
* installing *source* package 'leaflet' ...
** 成功将'leaflet'程序包解包并MD5和检查
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (leaflet)
下载的程序包在
‘C:\Users\r\AppData\Local\Temp\RtmpO65oT9\downloaded_packages’里
install.packages("sp")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
将程序包安装入‘C:/Users/r/AppData/Local/R/win-library/4.2’
(因为‘lib’没有被指定)
有二进制版本的,但源代码版本是后来的:
Binaries will be installed
trying URL 'http://cran.rstudio.com/bin/windows/contrib/4.2/sp_2.1-3.zip'
Content type 'application/zip' length 2028751 bytes (1.9 MB)
downloaded 1.9 MB
程序包‘sp’打开成功,MD5和检查也通过
下载的二进制程序包在
C:\Users\r\AppData\Local\Temp\RtmpO65oT9\downloaded_packages里
Now we need to load the packages to work in this R session
library(leaflet)
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
library(sp)
Warning: 程辑包‘sp’是用R版本4.2.3 来建造的
Q3.How many bike thefts were reported in 2021?
create a variable
biketheft = read.csv("D:/TA2025/Lab7/biketheft.csv")
biketheft
class(biketheft)
[1] "data.frame"
class(biketheft$Date);biketheft$Date <- as.Date(biketheft$Date, format="%m/%d/%Y")
[1] "Date"
sum
sum(format(biketheft$Date, "%Y") == "2021")
[1] 1447
Step 4: Ensure your longitude and latitude data is the correct data type Because your longitude and latitude data have decimal points, a numeric data type is most appropriate as discussed earlier. It is a good idea to ensure that this data is numeric, just in case it has been stored as a character string. To check the data type one can use the class() function like below
class(biketheft$Latitude)
[1] "numeric"
class(biketheft$Type)
[1] "character"
To force the numeric data type, you can use the following code:
biketheft$Longitude <- as.numeric(biketheft$Longitude)
biketheft$Latitude <- as.numeric(biketheft$Latitude)