Docsity

R Programming: Basic Commands, Vectors, Matrices, Data Frames, and Logic, Study notes of Descriptive statistics

Toggle sidebar
bg1
A.Elementary commands
> 33+17
[1] 50
> x<-42+8
> x
[1] 50
> x_42+8
Error: Object "x_42" not found
> x=59
> x
[1] 59
Arithmetic operators
+:plus -:minus *:times /: divided by ^: power
B.Vectors
> x<-c(1,2,3,4)
> x
[1] 1 2 3 4
> length(x)
[1] 4
> names<-c("Peter","Mary","Nancy")
> names
[1] "Peter" "Mary" "Nancy"
> mix<-c(names,55,33)
> mix
[1] "Peter" "Mary" "Nancy" "55" "33"
seq(lower,upper,increment)
> seq(1,5,1)
[1] 1 2 3 4 5
> seq(2,20,2)
[1] 2 4 6 8 10 12 14 16 1 8 20
> ?seq
> x<-c(seq(1,5,1),seq(4,20,4))
> x
[1] 1 2 3 4 5 4 8 12 16 20
> 1:5
[1] 1 2 3 4 5
rep(pattern, number of times)
> rep(5,3)
[1] 5 5 5
> rep(1:3,4)
[1] 1 2 3 1 2 3 1 2 3 1 2 3
> x<-rep(seq(3),3)
> x
[1] 1 2 3 1 2 3 1 2 3
> x<-rep(seq(3),c(1,2,3))
> x
[1] 1 2 2 3 3 3
> x<-rep(seq(3),rep(3,3))
> x
[1] 1 1 1 2 2 2 3 3 3
> x<-rep(seq(3),rep(3,3))
pf3
pf4
pf5

Partial preview of the text

Download R Programming: Basic Commands, Vectors, Matrices, Data Frames, and Logic and more Study notes Descriptive statistics in PDF only on Docsity!

A.Elementary commands

[1] 50

x<-42+ x [1] 50 x_42+ Error: Object "x_42" not found x= x [1] 59 *Arithmetic operators +:plus - :minus :times /: divided by ^: power

B.Vectors

x<-c(1,2,3,4) x [1] 1 2 3 4 length(x) [1] 4 names<-c("Peter","Mary","Nancy") names [1] "Peter" "Mary" "Nancy" mix<-c(names,55,33) mix [1] "Peter" "Mary" "Nancy" "55" "33" seq(lower,upper,increment) seq(1,5,1) [1] 1 2 3 4 5 seq(2,20,2) [1] 2 4 6 8 10 12 14 16 18 20 ?seq x<-c(seq(1,5,1),seq(4,20,4)) x [1] 1 2 3 4 5 4 8 12 16 20 1: [1] 1 2 3 4 5 rep(pattern, number of times) rep(5,3) [1] 5 5 5 rep(1:3,4) [1] 1 2 3 1 2 3 1 2 3 1 2 3 x<-rep(seq(3),3) x [1] 1 2 3 1 2 3 1 2 3 x<-rep(seq(3),c(1,2,3)) x [1] 1 2 2 3 3 3 x<-rep(seq(3),rep(3,3)) x [1] 1 1 1 2 2 2 3 3 3 x<-rep(seq(3),rep(3,3))

x [1] 1 1 1 2 2 2 3 3 3 x<-1: x[3] [1] 3 x[c(1,3)] [1] 1 3 x+ [1] 3 4 5 6 7 x* [1] 2 4 6 8 10 x/ [1] 0.5 1.0 1.5 2.0 2. x^ [1] 1 4 9 16 25 Common functions Sqrt(): square root; log(): natural logarithm; log10():logarithm base 10; Exp(): exponential; abs(): absolute value; round(): round to the nearest integer; Ceiling(): round up; floor(): round down; sin(), cos(), tan(); asin(), acos(), atan()

C.Matrices

Matrix(data,nrow,ncol,byrow=F)

x<-c(1,2,3) y<-c(4,5,6) xy<-matrix(c(x,y),nrow=2) xy [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 z<-1: matrix(z,nrow=4) [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 matrix(z,nrow=4,byrow=T) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15 [4,] 16 17 18 19 20 xy[2,3] [1] 6 xy[,1]

[1] 1 2

xy[2,] [1] 2 4 6 xy*xy [,1] [,2] [,3] [1,] 1 9 25 [2,] 4 16 36 xy/xy [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1

read.table("Sta135jiming/T1-1.dat") #file without column names! V1 V 1 3497900 0. 2 2485475 0. 3 1782875 0. 4 1725450 0. 5 1645575 0. 6 1469800 0. data2=read.table("Sta135jiming/T1-1.dat") mean(data 2 ) V1 V 2.101179e+06 5.143333e- 01 var(data 2 ) V1 V V1 5.894434e+11 5.737374e+ V2 5.737374e+04 7.016667e- 03 Read into a matrix: matrix(scan('c:\T1-5.dat'),ncol=6,byrow=T) Read 294 items [,1] [,2] [,3] [,4] [,5] [,6] [1,] 8 98 7 2 12 8 [2,] 2 7 107 4 3 9 [3,] 5 3 7 103 4 3 [4,] 5 6 3 10 88 5 .

. .

w<- matrix(scan('Sta135jiming/Wichern_data/T1-5.dat'),ncol=7,byrow=T) Read 294 items colMeans(w) [1] 7.500000 73.857143 4.547619 2.190476 10.047619 9.404762 3. var(w) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 2.5000000 - 2.7804878 - 0.3780488 - 0.4634146 - 0.5853659 - 2.2317073 0. [2,] - 2.7804878 300.5156794 3.9094077 - 1.3867596 6.7630662 30.7909408 0. [3,] - 0.3780488 3.9094077 1.5220674 0.6736353 2.3147503 2.8217189 0. [4,] - 0.4634146 - 1.3867596 0.6736353 1.1823461 1.0882695 - 0.8106852 0. [5,] - 0.5853659 6.7630662 2.3147503 1.0882695 11.3635308 3.1265970 1. [6,] - 2.2317073 30.7909408 2.8217189 - 0.8106852 3.1265970 30.9785134 0. [7,] 0.1 707317 0.6236934 0.1416957 0.1765389 1.0441347 0.5946574 0. data[,3:5] height weight age 1 50 120 20 2 70 140 30 3 45 100 45 4 80 200 32 5 100 190 32 mean(data[,3:5]) height weight age 69.0 150.0 31. var(data[,3:5]) height weight age height 505.0 887.5 - 21. weight 887.5 1900.0 - 67. age - 21.5 - 67.5 79.

is.matrix(data) [1] FALSE is.data.frame(w) [1] FALSE is.data.frame(data) [1] TRUE

E.logical expressions

< less than

greater than <= less than or equal to = greater than or equal to == equal to != not equal to & and | or ! not x<-seq(5,20,4) x [1] 5 9 13 17 x<-seq(5,20,5) x [1] 5 10 15 20 x< [1] TRUE TRUE FALSE FALSE sum(x<11) [1] 2 x[x<11] [1] 5 10