본문 바로가기
비트 장기/R

예제 11번~

by woohyun22 2018. 9. 3.

랜덤으로 값 받아오기


a<-sample(1:6,10,replace=T); a #1에서 6까지 랜덤하게 뽑는것(중복허용X)

#a<-sample(1:6,4,replace=F);a # 밑에 세개는 같은 코드이다.

#a<-sample(1:6,4,replace=FALSE);a

#a<-sample(1:6,4);a



필드 인덱스 뽑아내기 및 값 주기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
library(ggplot2)
 
a<-sample(1:10,20,replace=T); a #1에서 6까지 랜덤하게 뽑는것(중복허용X)
#a<-sample(1:6,4,replace=F);a # 밑에 세개는 같은 코드이다.
#a<-sample(1:6,4,replace=FALSE);a
#a<-sample(1:6,4);a
 
qplot(a,bins = 30)#  그래프 만들어내기 #qplot안에는 table가 들어갈수없다.
a<-sample(a); a 
table(b<-a); #테이블과 plot은 매우 밀접하다 항상 같이 쓰인다.
a>=6 # 비교한 결과를 보여준다. 이렇게 해서는 갯수에 대한 결과를 가질수없다.
 
 
c<-which(a>=6);c #결과를 만족한 값들의 index가 나온다.
length(which(a>=6));# 특정 필드안의 갯수가 몇개인지 구할때 쓴다. 
cs


테이블들과 필드 가지고 놀기


1
2
3
4
5
6
7
8
9
head(mpg)
qplot(mpg$cty,bins = 30)
dim(mpg)#테이블 갯수와 필드 갯수
summary(mpg)#전체 보여주기
 
연비3<-(mpg$cty+mpg$hwy);#연비1과 연비 2 합
summary(연비3)#정보 보여주기
mean1<-(mean(연비3));#평균값 넣어주
length(which(연비3>=mean1))#연비3에서 평균값보다 큰 애들의 갯수
cs


ifelse 수우미양가


1
2
3
4
5
6
7
8
9
10
11
library(ggplot2)
 
a<-c(1:10);a
b<-sample(1:100,10,replace = T);b #중복되는값도 괜찮
ifelse(3>2,"호랑이","코끼리")#r에서 제어문 사용방법
df = data.frame(a,b);
df$c<-ifelse(df$b>=90, "수",
                    ifelse(b>=80,"우",
                           ifelse( b>=70,"미",
                                   ifelse( b>=60,"양",
                                   "가"))));df
cs


midewest 가지고놀기


1
2
3
4
5
6
7
8
9
10
11
12
library("ggplot2")
 
a<-(midwest$popasian)
b<-(midwest$poptotal)
c<-(a/b)*100;c
d<-(mean(c))
ifelse(c>=d,"높다","낮다")
# a1<-(mean(a)); a1
# length(which(a>=a1))
qplot(c, bins = 30)
 
 
cs


filter 필터


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#install.packages("dplyr")#데이터 가공에 유용한 패키지
library("ggplot2")
library("dplyr")# 데이터 조건줘서 찾기가 매우 편하다. 이거 써야 필터가능
name<-c("장영실","이순신","이이","정약용","정우현","김구","유관순","김유신")
class<-sample(1:3,8,replace=T); b#학년
kor<-sample(1:100,8,replace = T)
eng<-sample(1:100,8,replace = T)
mat<-sample(1:100,8,replace = T)
df<-data.frame(name,class,kor,eng,mat)
dim(df)
str(df)
data.frame(df$name,df$mat)
 
#&  |, 필터 활용
 
df %>% filter(class==1#ctrl + shift + m 1학년 학생만 걸러서 보여준다.
df %>% filter(class!=1#1학년 뺴고 구하기 
df %>%filter(class==1&mat>=50)
df %>%filter(kor>=30 & kor<=80)
df %>%filter(kor<=30 | kor>=80)# or = |, and = &
df %>%filter(kor>=70 | mat<=70)
df %>%filter(class==3&kor<50,eng<50,mat<50)
 
df %>%filter(class==1)
cs




r1<-df %>%filter(class==1);r1


cat(sum(r1$kor),sum(r1$eng),sum(r1$mat))


filter랑 select 조지기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#install.packages("dplyr")#데이터 가공에 유용한 패키지
library("ggplot2")
library("dplyr")# 데이터 조건줘서 찾기가 매우 편하다. 이거 써야 필터가능
name<-c("장영실","이순신","이이","정약용","정우현","김구","유관순","김유신")
class<-sample(1:3,8,replace=T); b#학년
kor<-sample(1:100,8,replace = T)
eng<-sample(1:100,8,replace = T)
mat<-sample(1:100,8,replace = T)
df<-data.frame(name,class,kor,eng,mat)
 
df %>% select(kor); 
df %>% select(kor,name)#필드선택 명령 하나씩 원하는걸 가져와서 보여줄수있다.
df %>% filter(class==1) %>#줄을 내려서 사용하자
  select(name,mat)#filter로 정리하고 select로 골라서 보여줌
 
cs


붙여쓰기


df %>% 

  filter(class==1) %>% #줄을 내려서 사용하자

  select(name,mat)#filter로 정리하고 select로 골라서 보여줌


df %>% 

  filter(class==1 |class==2 | class==3) %>% #줄을 내려서 사용하자

  select(name,mat) %>% 

  head()#filter로 정리하고 select로 골라서 보여줌



728x90

'비트 장기 > R' 카테고리의 다른 글

9/5  (0) 2018.09.05
9/4  (0) 2018.09.04
R 예제  (0) 2018.09.01

댓글