Rlang(1)Introduction and Installation - Calculate and Load Data
Install R Script
Download the file from here http://www.go-parts.com/mirrors-usa/cran/, I get the version R-3.2.2.pkg.
Install Pandoc - Tool to Convert md to Documents
Download the latest package from here https://github.com/jgm/pandoc/releases/tag/1.15.0.6, I get the version pandoc-1.15.0.6-osx.pkg
Follow the docs here http://dapengde.com/r4dummies
1. Introduction
We use R language to do data process, draw diagram and programming.
Anyone can R - anyone can Er/2, haha.
I already install R-3.2.2.pkg, Need I install https://www.rstudio.com/products/rstudio/download/, RStudio, it is a nice tool.
After I install R-3.2.2.pkg, I can verify the version using following command.
> r --version
R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
2. Use R to do Calculation
Create a new R script file on Rsutdio, it will create a file named similar to Calculate.R, end with .R.
3 * (2 + 2)
sqrt(9)
## comments here.
Some other useful functions, round(), trunc(), sort(), abs(), exp(), log(), log10(), sin(), cos(), tan(), sin(), cos(), tan().
pi
## 3.141593
> options(digits=22)
> pi
[1] 3.141592653589793115998
> options(digits = 3)
> x <- c(61, 45, 55, 46, 56, 79, 86, 57, 56, 56, 57, 71)
> x[4]
[1] 46
Operation on x
> x + 100
[1] 161 145 155 146 156 179 186 157 156 156 157 171
These commands will directly draw the diagram for us.
options(digits = 3)
x <- c(61, 45, 55, 46, 56, 79, 86, 57, 56, 56, 57, 71)
x[4]
x + 100
plot(x)
## calculate the average
sum(x)/length(x)
mean(x)
Some other useful functions provided by R language, sum(), mean(), max(), min(), range(), median(), sd(), var().
3. Manage the Data from File
Reading the document from here http://dapengde.com/archives/14818, Download the sample data file from here
http://dapengde.com/wp-content/uploads/2013/03/dapengde_DummyR_PM25.csv
Put this in the command console, you will see the help information for read.table
>?read.table
This will read the file directly and open the file with default application
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
mydata
file.show(mydata)
load the csv data directly from file
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
mydata
##file.show(mydata)
#pm <- read.table(file=mydata, header = TRUE, sep=",")
pm <- read.csv(file=mydata)
The command summary will display a very useful information about the data
> summary(pm)
time h8 h100 h325
Min. : 0.00 Min. : 46.0 Min. : 32.0 Min. : 30.0
1st Qu.: 5.75 1st Qu.: 75.5 1st Qu.: 48.0 1st Qu.: 42.8
Median :11.50 Median : 93.5 Median : 67.5 Median : 54.5
Mean :11.50 Mean : 98.7 Mean : 72.1 Mean : 65.5
3rd Qu.:17.25 3rd Qu.:128.0 3rd Qu.:100.0 3rd Qu.: 88.5
Max. :23.00 Max. :150.0 Max. :123.0 Max. :126.0
1st Qu and 3rd Qu
https://zh.wikipedia.org/wiki/%E5%9B%9B%E5%88%86%E4%BD%8D%E6%95%B0
Get the row 10, column 2 data.
> pm[10, 2]
[1] 150
Pick up the C array of data with column 2
> pm[c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22), 2]
[1] 80 91 100 144 150 106 68 46 68 92 108
Generate the seq from seq()
> pm[seq(0,22,2),2]
[1] 80 91 100 144 150 106 68 46 68 92 108
All data belong to column 2
> pm[,2]
[1] 97 80 64 91 87 100 128 144 150 150 150 106 78 68 62 46 55 68 84 92 95
[22] 108 128 138
Pick up the column H8
> pm[, "h8"]
[1] 97 80 64 91 87 100 128 144 150 150 150 106 78 68 62 46 55 68 84 92 95
[22] 108 128 138
Similar to the command above
> pm$h8
[1] 97 80 64 91 87 100 128 144 150 150 150 106 78 68 62 46 55 68 84 92 95
[22] 108 128 138
List the Column Name
> names(pm)
[1] "time" "h8" "h100" "h325"
References:
http://www.go-parts.com/mirrors-usa/cran/
https://www.r-project.org/
http://www.biosino.org/R/R-doc/files/R4beg_cn_2.0.pdf
https://github.com/yihui/r-ninja
http://pandoc.org/
http://dapengde.com/r4dummies
http://dapengde.com/archives/14802