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 86113
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78623
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95338
42 MySQL 데이터베이스 기초 file 졸리운_곰 2018.07.05 1917
41 MySQL 기본 사용법 및 예제 졸리운_곰 2018.07.05 1200
40 COUNT() and GROUP BY 졸리운_곰 2018.07.05 1145
39 한 행에 중복된 값을 겹치지않게 count 해오는법(distinct , group by) 졸리운_곰 2018.07.05 1067
38 MYSQL GROUP BY 후 ROW COUNT file 졸리운_곰 2018.07.05 1173
37 group by로 해서 묶은 그룹의 count의 총 수(총 row수) 뽑기 file 졸리운_곰 2018.07.05 772
36 MySQL - 일별통계, 주간통계, 월간통계 졸리운_곰 2018.07.05 3015
35 mysql select 한 값을 insert 하는 sql 졸리운_곰 2018.07.02 1304
34 Auditing your MySQL Data 졸리운_곰 2018.07.02 1004
33 How To: Use MySQL triggers to log table changes 졸리운_곰 2018.07.02 1096
32 MySQL - History Tables 이력관리 / 히스토리 테이블 졸리운_곰 2018.07.02 2053
31 MariaDB 10의 NoSQL 기능과 MySQL의 Json 관련 UDF 졸리운_곰 2018.06.22 1307
30 [MySQL] Select 결과 Update하는 SQL 작성 file 졸리운_곰 2018.06.20 2470
29 조건에 맞게 select 한 후 update 시키기 졸리운_곰 2018.06.20 1150
28 MySQL (select) UPDATE file 졸리운_곰 2018.06.20 1065
27 MySQL에서 중복 값 찾기 졸리운_곰 2018.06.15 953
26 mysql case문 사용하기 졸리운_곰 2018.06.14 926
25 [MySQL] UPDATE 시 에러코드 1175 처리 file 졸리운_곰 2018.05.30 820
24 MySQL 데이터형 및 크기 졸리운_곰 2018.05.13 1124
23 MySQL OR MariaDB에서 프로시저(Procedure)를 만들어보자. 졸리운_곰 2018.03.25 1166
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED