Contents

1 襴 讌
2 覯″一 select
3 Vectors and assignment
4 Vector arithmetic
5 Generating regular sequences
6 Logical vector
7 Missing values
8 Character vector
9 Index vector; selecting and modifying subsets of a data set
10 Other types of objects


1 襴 讌 #

> options(digits=3)
> pi
[1] 3.14
> options(digits=4)
> pi
[1] 3.142
> 

2 覯″一 select #

x <- seq(1:10)

x[-1] #豌 覯讌  覓伎
x[-(3:5)] # 3~5 覯讌  覓伎
x[c(1,3,5)] #1,3,5覯讌 襷 select
x < 5 #TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
x[x < 5] #5覲企   select
x[x%%2 = 1] #襷
x[!is.na(x) & !is.null(x)] #na null  る..


3 Vectors and assignment #

覲 覯″郁 1, 2, 3 麹 覦覯 れ螻 螳.
> #覦覯1
> x <- c(1,2,3)
> x
[1] 1 2 3
>
> #覦覯2
> assign("y", c(1,2,3))
> y
[1] 1 2 3
>
> #覦覯3
> c(1,2,3) -> z
> z
[1] 1 2 3
> 
> #覯″一  螳 螻 覦覯
> 1/z
[1] 1.0000000 0.5000000 0.3333333
>
> #螳 豪 覲襯 伎 覲 
> a <- c(z, 0, z)
> z
[1] 1 2 3
> #覦覯4 scan() 伎
> x <- scan()
1: 1 2 3 4 5
6: 6 7 8 9 NA
11: 
Read 10 items
> x
 [1]  1  2  3  4  5  6  7  8  9 NA
> x <- scan()
1:   
危 scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan  'a real'襯 豸″螻 , 詞 蟆 ''給
> scan(what = "")
1:   
4: 
Read 3 items
[1] "" "" ""
> scan(what = complex(1)) #覲旧
1: 1 2 3
4: 
Read 3 items
[1] 1+0i 2+0i 3+0i
> x <- scan(what = numeric())
1: 1 2 3
4: 
Read 3 items
> x
[1] 1 2 3
> #c:\r_work.txt 殊 る 螳
> x <- scan(file = "c:\\r_work.txt")
Read 6 items
> x
[1] 1 2 3 4 5 6
> x <- scan(file = "c:\\r_work.txt", what = character(0)) #what 讌
Read 6 items
> x
[1] "1" "2" "3" "4" "5" "6"
> 

4 Vector arithmetic #

覯″一 蠍語願 るゴ覃 讌ъ 讓曙 豌覿 れ [1]. 蟆曙 2*x + y + 1
, x(1,2,3), y(1,2,3,0,1,2,3) ,
  1. 2 * x => 2, 4, 6
  2. 2*x + y => 2+1, 4+2, 6+3, 2+0, 4+1, 6+2, 2+3 => 3, 6, 9, 2, 5, 8, 5
  3. 2*x + y + 1 => 3+1, 6+1, 9+1, 2+1, 5+1, 8+1, 5+1 => 4, 7, 10, 3, 6, 9, 6
. (..襾碁Μ 譽 <;;)

> # 覯″一 蠍語願 るゴ覃 讌ъ 讓曙 豌覿 れ .
> x <- c(1,2,3)
> y <- c(x,0,x)
> v <- 2*x + y + 1
Warning message:
In 2 * x + y :
  longer object length is not a multiple of shorter object length
> v
[1]  4  7 10  3  6  9  6
> 

れ螻 螳 一磯 . sum() 企. x {1,2,3}企襦 sum(x) 襴願 1+2+3 蟆郁骸 6企. length() count. x {1,2,3}企襦 length(x) 襴願 3企. mean() 蠏企. 蠏碁覩襦 x [1,2,3}企襦 mean(x) 襴願 2企.
> sum((x-mean(x))^2)/(length(x)-1)
[1] 1
> sum(x)
[1] 6
> length(x)
[1] 3
> mean(x)
[1] 2
> 

るジ 襦 12 + 22 + ... + 102 れ螻 螳 螻壱 .
> sum(1:10^2)
[1] 5050
> 

覲旧 蟆曙磯 れ螻 螳 豌襴 .
> sqrt(-17)
[1] NaN
Warning message:
In sqrt(-17) : NANs螳 焔給

蠏碁覩襦 れ螻 螳 豌襴.
> sqrt(-17+0i)
[1] 0+4.123106i

5 Generating regular sequences #

Sequence襯 襷 覦覯 れ螻 螳.
> #覦覯1: 豐 Seqeunce
> 1:10 -> seq10
> seq10
 [1]  1  2  3  4  5  6  7  8  9 10
>
> #覦覯2: seed 譯手鍵
> seq(1, 10, by=2) ->seq10
> seq10
[1] 1 3 5 7 9
>
> #覦覯3: 企蟆 企 .
> seq10 <- seq(length=5, from=1, by=2)
> seq10
[1] 1 3 5 7 9
>
> #覦覯4: Seqeunce 覦覲
> x <- 1:3
> s <- rep(x, times=5)
> s
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
> seq(c(1,3,2))
[1] 1 2 3
> sequence(c(1,3,2)) #{1}, {1,2,3}, {1,2}
[1] 1 1 2 3 1 2
>
> #覦覯5: 螳螳 覦覲牛  .
> s <- rep(x, each=5)
> s
 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
> rep(3,3)
[1] 3 3 3
> 

6 Logical vector #

Logical vectors are generated by conditions.
> x <- c(1,2,3,4,5)
> x > 3
[1] FALSE FALSE FALSE  TRUE  TRUE
>
> x <- c(1,1,0)
> y <- c(0,1,1)
> x & y
[1] FALSE  TRUE FALSE
> x | y
[1] TRUE TRUE TRUE
> 

7 Missing values #

"Not available"企 詞朱 'NA'朱 れるゼ . is.na(x) x螳れ NA企 TRUE 覃 FALSE襯 襴危 . 碁語襦(;)朱 覓語レ 郁屋伎 . 讀, 碁語襦(;) 覓語レ 碁. (C語 螳 詞企手 覲企 .) 覓語レ 豌襴 殊曙 るジ讓曙企. 'NaN' "Not a Number" 曙襦 'NA' 襷谿螳讌襦 れ.
> z <- c(1:3, NA)
> ind <- is.na(z)
> z;ind
[1]  1  2  3 NA
[1] FALSE FALSE FALSE  TRUE
> z <- c(1:3, NA); ind <- is.na(z)
> z;ind
[1]  1  2  3 NA
[1] FALSE FALSE FALSE  TRUE
>
> Inf - Inf
[1] NaN
> 0/0
[1] NaN


8 Character vector #

Character vector 一危(") 一危(')襯 碁. 讌襷 一危 一危襦 襴壱碁. 襯 れ, "x-values", 'New iteration results" Character vector.
> name <- c("lee", "jae", "hak");
> name
[1] "lee" "jae" "hak"
> name <- c('lee', 'jae', 'hak');
> name
[1] "lee" "jae" "hak"
> 

Escape Character覓語 Cろ殊企. '\'覓語襯 . Quotes 襯 豺覃 襷 れ螻 螳 伎 覲 . 谿瑚襦 一危(") \"襦 . (\) 蟆曙磯 \\襦 \\襦 襴壱 .
\n newline 
\r carriage return 
\t tab 
\b backspace 
\a alert (bell) 
\f form feed 
\v vertical tab 
\\ backslash \ 
\nnn character with given octal code (1, 2 or 3 digits) 
\xnn character with given hex code (1 or 2 hex digits) 
\unnnn Unicode character with given code (14 hex digits) 
\Unnnnnnnn Unicode character with given code (18 hex digits) 

覓語 一一 paste()襯 覃 .
> x <- c("x", "y")
> paste(x, 1:10, sep="")
 [1] "x1"  "y2"  "x3"  "y4"  "x5"  "y6"  "x7"  "y8"  "x9"  "y10"
> 
> paste("proc", "ess", sep="/")
[1] "proc/ess"
> paste("proc", "ess", sep="")
[1] "process"
> 

9 Index vector; selecting and modifying subsets of a data set #

Index vector れ螻 螳 4螳讌襦 .
  • A logical vector
  • A vector of positive integral quantities
  • A vector of negative integral quantities
  • A vector of character strings

A logical vector
> x <- c(1,2,NA)
> y <- x[!is.na(x)] #NA螳  蟆れ y 麹企.
> y
[1] 1 2
> x <- c(0,1,NA)
> x[1]
[1] 0
> x[2]
[1] 1
> x[3]
[1] NA
> (x+1)[(!is.na(x)) & x>0] -> z #NA螳 螻 0覲企  蟆れ 伎 蠍 1 企.
> z
[1] 2
>

A vector of positive integral quantities
> x <- rep(c(1,2,2,1), times=4) #1,2,2,1 4覯 覦覲牛 vector襯 x 麹企.
> x
[1] 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1
> c("x", "y")[x]
 [1] "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x"
> 
> y <- c("x", "y")
> x <- c(1, 2, 3)
> y[x] #y 1覯, 2覯讌, 3覯讌 螳 豢ロ.
[1] "x" "y" NA 
> y <- c("x", "y", "z")
> y[x]
[1] "x" "y" "z"
> y[x & (x > 1)]
[1] "y" "z"
> 

A vector of negative integral quantities
> x <- c(1:10)
> x[1]
[1] 1
> x[9]
[1] 9
> x[-(1:5)]
[1]  6  7  8  9 10
> 

A vector of character strings
> fruit <- c(5, 10, 1, 20)
> names(fruit) <- c("orange", "banana", "apple", "peach")
> names
function (x)  .Primitive("names")
> names(fruit)
[1] "orange" "banana" "apple"  "peach" 
> lunch <- fruit[c("apple", "orange")]
> lunch
 apple orange 
     1      5 
> #願碓朱 覲企 ? numeric indices襯 蠍一牛朱.. 
> #"orange" "banana" "apple"  "peach"  fruit 螳れ {5, 10, 1, 20}襯 覲   企企. names attribute..

企一 蟆る .
> x <- c(-2, -1, 0, 1, 2, NA)
> x[is.na(x)] <- 0
> x
[1] -2 -1  0  1  2  0
> x[6] <- NA
> x
[1] -2 -1  0  1  2 NA
> x[x < 0] <- -x[x < 0]
危 x[x < 0] <- -x[x < 0] : 
  豌 豌覿 覿襯企 螳 NAs 讌 給
> x[6] <- 0
> -x[x < 0]
[1] 2 1
> x[x < 0] <- -x[x < 0]
> x
[1] 2 1 0 1 2 0
> paste("Page", 1:10)
 [1] "Page 1"  "Page 2"  "Page 3"  "Page 4"  "Page 5"  "Page 6"  "Page 7"  "Page 8"  "Page 9"  "Page 10"
> 


10 Other types of objects #

  • matrices or more generally arrays are multi-dimesional generalizations of vectors.
  • factors provide compact ways to handle categorical data
  • lists are a general form of vector in which the various elements need not be of the same type, and are often themselves vectors or list
  • data frames are matrix-like structures, in which the columns can be of different types. Think of data frame as 'data matrices' with on row per observational unit but with (possibly) both numerical an categorical variables.
  • functions are themselves objects in R which can be stored in he project's workspace.