R 실습 학습 2017-10-26 레슨1

# inflearn Kim So Dam lecture 2017-10-26

#### Data Structures ####

#### vector : 1 demention ####
# c() : combine
a1 <- c(1,2,3,4,5)
a1
length(a1)

# subsetting #
a1[2]
a1[2:4]
a1[-2]
a1[-c(3,5)]

a2 <- 1:5 # creating sequence
a2


a3 <- seq(1,5)
a3

?seq

seq(1,5,2)  # seq(from=1, to=5, by=2)

a4 <- rep(5,2) # rep(5, times=2)
a4

a5 <- rep(6:7, 2)
a5

?rep
a6 <- rep(6:7, each = 2)
a6

a <- c(a2, a4, a5)
a

length(a)

class(a)
mode(a)


# sequence
seq(0,100, 25)
seq(0,100, length.out - 5)  #length for ther output sequence
diff(seq(0,100, length.out - 5))

# recycling rule
b1 <- seq(1,10, by=2) #seq(1,10,2)
b1
length(b1)

b2 <- 1:10
b2

length(b2)

b3 <- b1 + b2
b3

length(b3)

b4 <- b2 / b1
b4
b4 <- round(b4, 2) # round(b4, digiths=2)
b4
?round
cbind(b1,b2,b3,b4)
class(cbind(b1,b2,b3,b4))
?cbind


# comparing vectors
i <- 3
i == 3   # == : test for equality -> returns boolean value
i <- pi
i > 3 # > : bigger than
i >= 3 # >= : bigger or equal to
i <= 3 # <= : smaller or equal to
i < 3 # < : smaller than

i1 <- c(0,1,2,3,pi,4,5)
i2 <- c(0,1,2,3,pi,pi,5)
class(i1)
length(i1)
i1 == i2
i1 != i2 # != : not equal to
i1 < i2
i1 <= i2
i1 > i2
i1 >= i2
which(i1 == i2)  # which()

i1[6]

any(i1 == i2)  # return TRUE if any value of i1 equal pi
all(i1 == pi)  # return TRUE if all value of i1 are pi

i3 <- 1:100
i3 < 3 # TRUE whenever i3 is less than 3
which(i3 < 3)
i3[i3 < 3]
i3[which(i3<3)]
i3[i3 %% 2 == 0]  # return even number elements
i3[i3 > median(i3)] # select all elements greater than the median

i3 <- 100:1
i3
i3 %% 2 == 1
i3[i3 %% 2 == 1]
i3[i3 %% 2 == 0]
i3 %% 2 == 0

i3 <- c(101:2)
i3
i <- 1:10
i[c(T,T,T,T,T,F,F,T,F,T)] # ture로 된된것된것만 출출력


# vectpr arithemetic
i4 <- 1:5
i5 <- 11:15
i4 + i5
i5 - i4
i4 * i5
i5 / i4
i5 ^ i4
log(10^2)
sqrt(i4)
log(i4)

# List Data Type

mylist <- list(name='John', age=24, myvector=c("a", "b", "c"))
mylist
str(mylist)
mylist[1]
mylist[[1]]
mylist$name
class(mylist)
class(mylist$name)

#### Matrix: 2d, Homogeneous ####
?matrix

mat1 <- matrix(c(1:10, rep(1:5,2), seq(1,20, by=2)), nrow=10, ncol=3,
               byrow= TRUE, dimnames = list(c(letters[1:10]), c("one", "two","three")))

mat2 = matrix(c(1:10, rep(1:5,2), seq(1,20,by=2)), 10, 3,
              dimnames=list(c(letters[1:10]), c("one", "two", "three"))
              )

mat1

mat2

mat1

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

class(mat1)

dim(mat1)

# dim(mat1) <- c(3,10)

a <- 1:20
dim(a) <- c(4,5)
a
?dim
mat1[1,]    # index by row
mat1[,1]   # index by column
mat1[3,3]
mat1[1:5,] # index first five rows
mat1[c(1,3),] # index first and third rows
mat1[c(1,3), 2:3]  # index second and third columns from first and third rows
mat1[,3]
mat1[,3] <- sample(c(0,1), 10, replace = TRUE) # replace column with new values

# ?sample

#### factors ####

?factor
# a factor is a vector object used to specify a discrete classification of the
# components of other vectors of the same length.
# level : each unique value in a vector
LETTERS
alphabet1 <- factor(LETTERS)
alphabet1

alphabet2 <- factor(LETTERS[1:10])
alphabet2
str(alphabet2)

alphabet3 <- factor(LETTERS[1:10], levels=LETTERS)
alphabet3 <- ordered(, levels =)
alphabet3
str(alphabet3)
class(alphabet3)

?factor

g1 <- c("a", "b", "b", "a")
class(g1)
mode(g1)

g1

g <- factor(c("a", "b", "b", "a"))
g
unique(g)
class(g)
mode(g)
levels(g)
g[3]
g[3] <- c("c")

g
levels(g) <- factor(c("a", "b", "c"))
levels(g)

number <- 1:3
num2 <- factor(number)
num2
num3 <- as.vector(num2)
class(num3)

# or

g2 <- factor(c("a", "b", "b", "a"), levels=c("a", "b", "c"))
g2[5] <- c("c")
g2

summary(g2)

#### Data Frame : 2d, Heterogeneous ####
name <- c("Ellie", "Taylar", "Luke", "Drake")
class(name)
gender <- c("Female", "Female", "Male", "Male")
class(gender)
age <- c(20,25,26,23)
class(age)
weight <- c(50, 50, 70, 75)
friends <- data.frame(name, gender, age, weight, stringsAsFactors = F)
friends
friends$gender
friends$gender <- factor(friends$gender)
str(friends)
dim(friends)
class(friends)
summary(friends)

ncol(friends)
nrow(friends)

colnames(friends)
rownames(friends)
friends$name
friends[,1]
friends[1,]
friends2 <- cbind(friends, data.frame(year=c(2,5,7,3)))
friends2
friends2[6]
class(colnames(friends))

df <- data.frame("x"=1:3, "y"=c("a", "b", "c"), z= 3*1:3-1)

df

x <- 1:3
y <- letters[x]
z <- 3*x-1
df <- data.frame(x,y,x)

df

# stringAsFactors = FALSE : not transform character into factor
friends1 <- data.frame(name, gender, age, weight, stringsAsFactors = T)
friends1
str(friends1)
friends1$gender <- factor(friends1$gender)
summary(friends1)
length(friends2)
friends2[6]

 

#### irisi data example ####

data(iris)
str(iris)
head(iris)
head(iris, 10)
tail(iris)
View(iris)
summary(iris)

mytable <- table(iris$Species) # table()
mytable
str(mytable)
names(mytable)
df <- data.frame(mytable)
df
mytable[2][[1]]
mytable[[2]]
mytable[2][1]
mytable[2]

# draw samples
dim(iris)
nrow(iris)
ncol(iris)
irissample <- sample(1:nrow(iris), 10) # nrow() : number of rows
irissample
iris[irissample,]

# different ways of indexing
colnames(iris)  # names(iris)
iris[10:20, "Sepal.length"]
iris[10:20, 1]
irissepal.Length[10:20]


# subset
setosa1 = subset(iris, Species == 'setosa')
head(setosa1)
summary(setosa1)
setoda2 <- iris[iris$Species == "setoda"]
setosa3 <- iris[which(iris$Species == "seotosa"), ]
which(iris$Species == 'setosa')
iris2 <- subset(iris, Species == "setosa", select = c(Sepal.Length, Species))
head(iris2)

setosavirgnica <- iris[which(iris$Species %in% c("setora", "virginica") & iris$Sepal.Width > 3.5),]
str(setosavirgnica)
summary(setosavirgnica)


# explore variables
# summary() : distributions for numeric variables, frequency for categorical variables
summary(iris)
quantile(iris$Sepal.Length)  # 꽃 받 침
quantile(iris$Sepal.Length , c(0.1, 0.5, 0,9)) # designate quartiles
var(iris$Sepal.Length)  # variance of Sepal.Length
sd(iris$Sepal.Length)

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86307
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78759
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95507
22 Docker에서 SQL Server 컨테이너 이미지 구성 file 졸리운_곰 2020.01.23 1554
21 MSSQL 설치형 한글 환경으로 변경 file 졸리운_곰 2020.01.23 2886
20 PRIMARY KEY 와 FOREIGN KEY 를 전부 뽑아주는 쿼리 졸리운_곰 2018.12.16 1773
19 [MSSQL] CASE 문 . 조건에 따라 값 정하기 ! CASE WHEN THEN 졸리운_곰 2018.07.24 1568
18 Track Data Changes (SQL Server) file 졸리운_곰 2018.07.02 1302
17 Docker가 있는 SQL Server 2017 컨테이너 이미지를 실행 하는 빠른 시작 file 졸리운_곰 2018.06.26 1055
16 [MSSQL] Management Studio 이용해 데이터베이스 생성하기 file 졸리운_곰 2018.06.17 1283
15 [MSSQL - GROUP BY HAVING 을 이용한 중복 데이타 체크] file 졸리운_곰 2018.06.15 1280
14 [SQL] select 한 결과로 update 처리, SQL한문장, How to UPDATE from SELECT in SQL Server 졸리운_곰 2018.01.22 1472
13 UNION으로 결과 집합 조합 졸리운_곰 2017.08.27 1341
12 uniqueidentifier(Transact-SQL) file 가을의곰 2017.06.10 1755
11 하위 쿼리를 사용하여 다른 쿼리 또는 식에 쿼리 중첩 [MS-ACCESS : ms offce suit] 가을의곰 2017.06.10 1639
10 [MS-SQL] 테이블명, 컬럼명 검색 졸리운_곰 2017.04.17 1934
9 DB의 모든 테이블에서 데이터 검색 졸리운_곰 2017.04.17 1717
8 Microsoft SQL Server DBA 가이드-DBA라면 이정도는 알아야한다!!! file 졸리운_곰 2017.01.15 1302
7 SQL Server DBA 가이드 file 졸리운_곰 2017.01.15 1685
6 IDENTITY_INSERT가 OFF로 설정되면 ‘테이블명’ 테이블의 ID 열에 명시적 값을 삽입할 수 없습니다 file 졸리운_곰 2017.01.15 1503
5 MS SQL 서버에서 자동증가, autoincrement 처리 file 졸리운_곰 2017.01.15 1773
4 MS SQL 서버의 날짜, 시간 => 문자열 변환 포멧 설명 졸리운_곰 2017.01.15 1215
3 MS SQL 서버 코딩 표준 가이드 file 졸리운_곰 2017.01.14 1654
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED