3 + 2
## [1] 5
pi
## [1] 3.142
Working Directory
getwd()
## [1] "C:/Users/ms/Desktop/R intro"
setwd("C:\\Users\\ms\\Desktop\\R intro")
#R command assignment, “<-” or “=” , 하지만 “="보다 ”<-“를 습관화하는게 좋음 #Semicolon(;) separates multiple commands.
beta.0 <- 3
beta.1 <- 2
rnorm(100)
## [1] 1.615467 -1.808191 -0.396103 1.018706 1.355181 0.992417 -0.149195
## [8] 0.572773 0.103315 -1.751043 0.871835 -0.306933 1.389622 0.789391
## [15] -0.939367 0.967248 -2.294039 1.528723 -2.251428 0.298459 -0.785506
## [22] 1.371435 -1.513685 0.001857 0.016300 0.139875 -0.635470 0.358098
## [29] 1.152141 -0.409704 -0.015991 -0.299920 1.390164 0.881419 0.030344
## [36] 0.769484 -0.709153 1.345010 -0.238591 0.377869 -0.614785 -1.246541
## [43] -1.036101 -0.392603 0.179797 0.794232 0.662346 2.246059 -1.802768
## [50] 0.435397 1.125045 -0.541882 1.860645 0.806238 -0.199188 -0.357933
## [57] -0.417338 -2.742612 0.359901 0.603641 -1.053093 -0.310098 -1.403212
## [64] 1.810945 0.530417 -0.467363 -0.108674 0.714985 -0.564988 1.369558
## [71] 0.656187 -0.773008 0.558007 2.290124 1.668110 0.337929 -0.418746
## [78] 0.431213 -0.983064 1.020301 -1.812867 -0.372473 0.257109 -0.135398
## [85] -0.281673 -0.311843 0.565227 -0.459108 -0.322975 0.458124 -0.501852
## [92] -1.787882 0.246637 -0.971882 -1.594284 1.148936 -0.358317 0.686273
## [99] -0.135903 -0.326965
대소문자 구분, assignmet”==“
a <- 1
A <- 2
a == A
## [1] FALSE
생선된 변수를 확인하기
ls()
## [1] "a" "A" "beta.0" "beta.1"
Data Types
x <- c(1, 2, 3, 4, 5)
y <- c("a", "b", "c")
x[3]
## [1] 3
Sequence
x <- seq(from = 0, to = 10, by = 0.1)
y <- seq(from = 0, to = 1, length = 21)
z <- 1:10
rep(1, 10)
## [1] 1 1 1 1 1 1 1 1 1 1
x
## [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3
## [15] 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7
## [29] 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1
## [43] 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5
## [57] 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9
## [71] 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3
## [85] 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7
## [99] 9.8 9.9 10.0
y
## [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65
## [15] 0.70 0.75 0.80 0.85 0.90 0.95 1.00
z
## [1] 1 2 3 4 5 6 7 8 9 10
Array
z <- array(1:20, dim = c(4, 5)) #(4,5) matrix
z[4, 5]
## [1] 20
Matrix
z <- matrix(1:20, 4, 5)
A <- matrix(2, 4, 5)
List
Hwang <- list(first.name = "myeong sik", age = 26, citizenship = "South Korea")
Hwang$age
## [1] 26
Hwang$first.name
## [1] "myeong sik"
# or
attach(Hwang)
age
## [1] 26
first.name
## [1] "myeong sik"
# or
Hwang[1]
## $first.name
## [1] "myeong sik"
#Logical vectors
x <- 1:10
y <- rep(5, 10)
z <- x < 5 #less than
sum(z)
## [1] 4
x == 5 #equal
## [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
x != 5 #not equal
## [1] TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
(x > 5) & (y < 2) #and
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
(x < 5) | (y < 2) #or
## [1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#Missang values 확인
x <- c(1, 2, 3, NA, 5)
is.na(x)
## [1] FALSE FALSE FALSE TRUE FALSE
#Read data
# From a file :read.csv()
x <- read.csv("C:\\Users\\ms\\Desktop\\R intro\\sample.csv", header = T)
package 불러오기
library("MatchIt")
## Loading required package: MASS
## ##
## ## MatchIt (Version 2.4-20, built: 2011-10-24)
## ## Please refer to http://gking.harvard.edu/matchit for full documentation
## ## or help.matchit() for help with commands supported by MatchIt.
## ##
data("lalonde")
# Want the list of built-in datasets contained in the currently loaded
# packages?
data()
#Export data
write(x, file = "output.txt", sep = ",")
## Error: 인수 1 (유형 'list')은 'cat'에 의해서 처리 될 수 없습니다
x <- matrix(1:20, 4, 5)
write.table(x, file = "table.txt")