R Introduction
by mervyn
source “K-MOOC 오세종 교수님의 [R 데이터 분석 입문] 강좌의 1.Introduction 중(http://www.kmooc.kr/courses/course-v1:DKUK+DKUK0003+2020_T3)”
R
- Data analysis & Statistics
- Calculator
- Similar to Programming language
- Basic Programming
- Link with Java, C
- Visualization
Calculation
| Basic Arithmetic | +, -, *, /, ^, %%(mod) |
| Comments | # |
| Function | log(), sqrt(), max(a, b) |
Variable
Temporary place to store value
a<- 10
b<- 20
c<- a+b
c
a<- "A" #10 is erased and A is stored
# now a+b occurs error
d<- 10; e<- 15; f<- 20 # One command for one line
Do not declare data type of variable Store any value, data type Use ‘’, “ “ for character data type
Naming Rule
- First word of a variable should be a character of .(dot)
- Cannot start with number or special marker
- After first word, character, number, dot, underline are available
- Case-Sensitive
Assign Value
<-
=
Once created and used, a variable does not perish until close R
A # Ctrl+Enter after typing the name of the variable
print(A) # print the content of the variable
Various data type of value can be stored in one variable
Shortcut
”<”: Alt - “Console”: Ctrl 2 “Script”: Ctrl 1
Data type
Numeric: integer, float, minus Character: character, string Logical: TRUE, FALSE (Case sensitive. T, F) Special marker: use without quotation
- NULL : has no value. No data type, 0 length
- NA: missing value. Logical constant, length 1. No data value is stored for the variable in an observation, even though it should be. (Wikipedia)
- NaN: Not a Number. Numeric data type. Mathematically undefinable value.
- inf, -inf: \(\infty\), \(-\infty\)
Vector
Data Structure that can store 1 dimensional array (in R) C Function: store vector to variable
x<- c(1, 2, 3)
y<- c("a", "b", "c")
z<- c(TRUE, TRUE, FALSE, FALSE)
x
y
Same data type for the values assigned for vector
Other Functions
- ”:” Integer: Integer Input integers in a row
v1<- 50:90 # Integers between 50 and 90
v1
v2<- c(1, 2, 5, 50:90) # 1, 2, 5, and integers between 50 and 90
v2
- “seq” (sequence) Vector with numbers with steps
v3<- seq(1, 101, 3) # (start, end, step)
v3
v4<-seq(0.1, 1.0, 0.1)
- “rep” (repeat) Store one value recursively
v5<- rep(1, times=5)
v5
v6<- rep(1:5, times=3) # 1 to 5 for 3 times
v6
v7<- rep(c("a", "b", "c"), each=3) # iterate each of a, b, and c for 3 times
v7 # "a" "a" "a" "b" "b" "b"...
v8<- rep(c("a", "b", "c"), times=3) # iterate a, b, and for 3 times
v8 # "a" "b" "c" "a" "b" "c"...
- names Naming the continuing values of the vector Does not affect the calculation
score <-c(90, 85, 70) # score
names(score)<-c("John", "Tom", "Jane")
# print values with names
score
names(score)[2] # print 2nd name of names
score[2] # print 2nd value
- index Integer to point the value of the specific location in the variable. Starting from 1 in R.
- negative index Exclude the value of the specific location
d<- c(1, 4, 3, 7, 8)
d[1:3] # print value of d[1] to d[3]. Continuous
d[c(1, 3, 5)] # print value of d[1], d[3], and d[5]. Non-continuous
d[se1(1, 5, 2)] # print odd number data
d[-2] # exclude d[2]
d[-c(3:5)] # exclude d[3] to d[5]
- Print the value by name
GNP<- c(2090, 2450, 960) # GNP
names(GNP)<- c("Korea", "Japan", "Nepal")
GNP[1]
GNP["Korea"]
GNP[c("Korea", "Nepal")]
Comments
Post comment