官术网_书友最值得收藏!

Setting up the R environment and packages

R is a free software under GNU open source license. R comes with a basic package and also has a large number of user-contributed packages for advanced analysis and modeling. It also has a nice graphics user interface-based editor called RStudio. In this section, we will learn how to download R, set up the R environment in your computer, and write a simple R program.

Installing R and RStudio

The Comprehensive R Archive Network (CRAN) hosts all releases of R and the contributed packages. R for Windows can be installed by downloading the binary of the base package from http://cran.r-project.org; a standard installation should be sufficient. For Linux and Mac OS X, the webpage gives instructions on how to download and install the software. At the time of writing this book, the latest release was version 3.1.2. Various packages need to be installed separately from the package page. One can install any package from the R command prompt using the following command:

install.packages("package name")

After installing the package, one needs to load the package before using it with the following command:

library("package name")

A very useful integrated development environment (IDE) for R is RStudio. It can be downloaded freely from http://www.rstudio.com/. RStudio works on Windows, Linux, and Mac platforms. It has both a desktop version and also a server version that can be used for writing R programs through a browser interface on a remote server. After installing R and RStudio, it is useful to set the default working directory to the directory of your choice. RStudio reads and writes files containing R codes into the working directory. To find out what the current directory is, use the R command getwd( ). To change the working directory to a directory of your preference, use the following command:

setwd("directory path")

You can also set this from the menu bar of RStudio by clicking on Session | Set Working Directory.

Your first R program

Let us write a simple program to add two integers x and y resulting in their sum z. On the command prompt in RStudio, type the following commands and press Enter:

>x <-2
>y <-3
>z <-x+y
>print(z)
[1]  5

Now, you can assign different values to x and y and print z to see how z changes. Instead of print(z), you can also simply enter z to print its values.

主站蜘蛛池模板: 辽阳市| 浙江省| 启东市| 乌苏市| 浦东新区| 鹰潭市| 沛县| 察隅县| 桂林市| 鄱阳县| 出国| 彭阳县| 凤庆县| 福安市| 日喀则市| 抚顺市| 平顶山市| 绿春县| 双城市| 东宁县| 桃园市| 鸡泽县| 招远市| 铜梁县| 台南市| 金溪县| 延边| 杂多县| 葫芦岛市| 武鸣县| 娄底市| 惠东县| 亚东县| 阜康市| 本溪| 宜川县| 思茅市| 会昌县| 浦县| 林口县| 枣强县|