"R 그룹별 건수 구하기"의 두 판 사이의 차이

25번째 줄: 25번째 줄:
<source lang='r'>
<source lang='r'>
library(sqldf)
library(sqldf)
sqldf("SELECT fruit, COUNT(ea) FROM df GROUP BY fruit")
sqldf("SELECT fruit, COUNT(ea) AS cnt FROM df GROUP BY fruit")
##    fruit COUNT(ea)
##    fruit cnt
## 1  apple         3
## 1  apple   3
## 2 banana         2
## 2 banana   2
## 3 orange         1
## 3 orange   1
</source>
</source>



2019년 8월 25일 (일) 19:16 판

1 개요

R 그룹별 건수 구하기
  • 대략 "SELECT fruit, COUNT(ea) FROM df GROUP BY fruit"
df <- read.csv( header=T, stringsAsFactors=F, text="
day,fruit,ea
2019-06-01,apple,1
2019-06-11,apple,1
2019-06-21,banana,2
2019-07-01,apple,3
2019-07-11,banana,4
2019-08-25,orange,2
")

aggregate(ea ~ fruit, df, length)
##    fruit ea
## 1  apple  3
## 2 banana  2
## 3 orange  1

table(df$fruit)
##  apple banana orange 
##      3      2      1
library(sqldf)
sqldf("SELECT fruit, COUNT(ea) AS cnt FROM df GROUP BY fruit")
##    fruit cnt
## 1  apple   3
## 2 banana   2
## 3 orange   1

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}