빅데이터/R

[R] 파이프 연산자(pipe)

kerpect 2020. 7. 3. 14:28
728x90
반응형
SMALL

파이프 연산자(pipe) 

:  파이프 연산자는 이름 그대로 어떤 값들이 파이프를 통과하는 것처럼 함수와 함수들을 타고다닐 수 있게 해줍니다.

%>% : 파이프(pipe) 연산자 - (단축키 : ctrl, shift , m )

 

위의 데이터를 활용하여서 설명하겠습니다. 

 

① 예제 

- filter()

exam %>% filter(class == 1) # 객체를 통해서 접근하기 때문에 변수를 명시하지 않아도 됩니다.  

		- 출력값 - 
  id class math english science
  1     1   50      98      50
  2     1   60      97      60
  3     1   45      86      78
  4     1   30      98      58
  

 

② 예제

- select()

 exam %>% select(class, math, english)
 
      - 출력값 - 
      
   class math english
     1   50      98
     1   60      97
     1   45      86
     1   30      98
     2   25      80
     2   50      89
     2   80      90
     2   90      78
     3   20      98
     3   50      98
     3   65      65
     3   45      85
     4   46      98
     4   48      87
     4   75      56
     4   58      98
     5   65      68
     5   80      78
     5   89      68
     5   78      83

 

③ 예제 

- filter() + select() 

 class가 1인 행만 추출한 다음 enlish 추출
 
exam %>% filter(class==1) %>%  select(english)


   - 출력값 - 
    english
1      98
2      97
3      86
4      98

 

④ 예제

- 과목별 총점과 총점 기준 정렬해서 상위 6개 데이터만 출력

exam  %>%  
  mutate(total = math + english + science) %>% 
  arrange(desc(total)) %>% 
  head

                - 출력값 - 
   id  class math english science total
   18     5   80      78      90   248
   19     5   89      68      87   244
    6     2   50      89      98   237
   17     5   65      68      98   231
   16     4   58      98      65   221
   20     5   78      83      58   219

 

 

 

728x90
반응형
LIST