아이티윌_데이터 분석 55기/문제풀이_통계 및 분석

#6-2. 6일차 퀴즈에 대한 문제풀이

ecosso 2026. 4. 7. 18:00

 1. act_data_1.csv 파일을 읽고
 1) user별 모든 행동의 총 합을 출력하고 이 중 5번째로 많은 user의 userid 출력

 

act1 <- read.csv('act_data_1.csv')
nrow(act1)
length(unique(act1$user_id))

더보기

[내 답안]

 

head(act1)
str(act1)


act1$total <- apply(act1[3:18], 1, sum)
act1$rank <- rank(-act1$total)

act1[act1$rank == 5,]
act1$user_id[act1$rank == 5]

> act1[act1$rank == 5,]
       user_id       date act_1_cnt act_2_cnt act_3_cnt act_4_cnt act_5_cnt act_6_cnt act_7_cnt act_8_cnt act_9_cnt act_10_cnt act_11_cnt
134021    7888 2010-03-19         4        29         0         0         0        59         0         0         0          2          6
       act_12_cnt act_13_cnt act_14_cnt act_15_cnt act_16_cnt act_17_cnt total rank
134021         12          2          0          6       1274        364  1394    5
> act1$user_id[act1$rank == 5]
[1] 7888

 

[문제풀이]

 

act1[,3:ncol(act1)]
act1$total_cnt <- rowSums(act1[,-c(1,2)])

act1_1 <- ddply(act1, .(user_id), summarise, total = sum(total_cnt))

res1 <- dplyr::arrange(act1_1, desc(total))[5,'user_id']
print(res1)

> res1 <- dplyr::arrange(act1_1, desc(total))[5,'user_id']
> print(res1)
[1] 7829

 

* user_id가 중복될 수 있었으므로 그룹핑을 해줬어야함

 

 2) 요일별 act_1_cnt가 가장 많은 user를 확인, 이 user들의 모든 행동지수의 총합 출력

더보기

[내 답안]

 

head(act1)

as.Date(act1$date, '%Y-%m-%d')
act1$date2 <- strftime(act1$date, '%A')
head(act1)

aggregate(act1$act_1_cnt, list(act1$date2), max)

df2 <- ddply(act1, .(date2), subset, act_1_cnt == max(act_1_cnt))
df2$total_index <-apply(df2[3:19], 1, sum)

print(df2)

 

> print(df2)
   user_id       date act_1_cnt act_2_cnt act_3_cnt act_4_cnt act_5_cnt act_6_cnt act_7_cnt act_8_cnt act_9_cnt act_10_cnt act_11_cnt act_12_cnt
1    11008 2010-03-05        11        23         0         1         1        26         0         0         3          2          1          3
2    11138 2010-03-12        11        33         1         1        48        50         0         0         1          8          3         11
3    10992 2010-03-04        11        28         0         1         1        26         0         0         0          1          1          3
4    10996 2010-03-04        11        28         1         2        14        42         0         0        39          9          1         12
5    10995 2010-03-04        11        31         0         1         6        38         0         0         6          4          1          4
6    10998 2010-03-04        11        16         0         1        20        16         0         0         8          3          0          7
7    11130 2010-03-11        11        19         0         1         5        10         0         0         0          0          0          0
8    11262 2010-03-25        11        15         0         1         1         4         0         0         1          1          0          0
9     5931 2010-03-31        12         7         0         1         8         3         0         0         0          0          1          4
10   11300 2010-03-31        12        32         1         2        30        44         0         0         0         11          4         11
11   11307 2010-03-31        12        31         0         0        31        36         0         0        14          8          2          7
12     787 2010-03-31        12         6         0         0         6         1         0         0         0          0          0          0
13    5994 2010-03-31        12        10         0         1        19        14         0         0         1          1          1          5
14    6432 2010-03-31        12         4         0         0         6         5         0         0         0          0          1          6
15   11301 2010-03-31        12        26         0         0        17        33         0         0         1          3          2          5
16   11276 2010-03-31        12        24         1         2        66        41         0         0         1          7          5         34
17     839 2010-03-31        12         7         0         2         3         3         0         0         0          0          1          0
18    1618 2010-03-31        12         8         0         0         1         1         0         0         0          0          0          0
19   11303 2010-03-31        12        17         0         0        27        20         0         0         0          1          1          3
20    3587 2010-03-31        12        12         0         0         2        10         1         0         1          1          1          7
21   11306 2010-03-31        12        36         0         0        39        40         0         0         2          3          1          8
22    1223 2010-03-31        12         7         0         0        25         3         0         0         0          0          1          0
23   10642 2010-03-01        11        14         0         0         0        19         0         0         0          0          0         17
24   11287 2010-03-29        11        32         0         1        17        46         0         0         7          6          2          8
25   11050 2010-03-07        13        29         0         1        32        24         0         0         2          7          2          2
26   11061 2010-03-07        13        17         0         1         4        19         0         0         1          1          1          2
27   11035 2010-03-07        13        20         0         1         2        14         0         0         1          2          1          1
28   11159 2010-03-14        13        47         0         2        39        64         0         0         0          4          4         10
29   11163 2010-03-14        13        21         0         1        23        25         0         0        19          1          1          2
30   11152 2010-03-13        11        28         0         1        14        34         0         0         3          3          1          4
31    9934 2010-03-13        11        14         0         1        31        10         0         0         1          3          0          0
32   11148 2010-03-13        11        27         0         1        10        32         0         0         0          0          2          4
33   10438 2010-03-13        11        13         1         1         9        11         0         0         4          5          0          1
34   11271 2010-03-27        11        31         0         1         6        36         0         0         0          2          0          7
35   11268 2010-03-27        11        41         0         2        26        56         2         0         6          4          3          7
36   11274 2010-03-27        11        16         0         1         3        12         0         0         0          0          0          0
37   10982 2010-03-02        11        11         0         0         1        11         0         0         0          4          2          5
38   11109 2010-03-09        11        22         0         1        11        25         0         0         1          0          1          4
39   11243 2010-03-23        11        35         0         1        19        53         0         0         1          2          1         13
   act_13_cnt act_14_cnt act_15_cnt act_16_cnt act_17_cnt  date2 total_index
1           0          3          6         47         41 금요일         168
2           0          4          6        116        129 금요일         422
3           0          3          6         16         21 목요일         118
4           0          4          6         68         81 목요일         318
5           0          5          6        114         73 목요일         300
6           0          3          3         87         30 목요일         205
7           0          4          3          5          4 목요일          62
8           0          3          1         45         19 목요일         102
9           0          1          0         15          8 수요일          60
10          0          8         14        138         82 수요일         389
11          0          5          0        167         57 수요일         370
12          0          1          1          0          0 수요일          27
13          0          2          2         35         52 수요일         155
14          0          1          0          6          1 수요일          42
15          0          6          5         99         58 수요일         267
16          1          6         17         29        204 수요일         450
17          0          1          1          0          0 수요일          30
18          0          0          1          1          3 수요일          27
19          0          6          5         59         30 수요일         181
20          0          1          3         88         45 수요일         184
21          0          5          1        102         60 수요일         309
22          0          2          0          0          0 수요일          50
23          0          0         27        346        200 월요일         634
24          0          3          6        113         56 월요일         308
25          0          5         10         22         66 일요일         215
26          0          3          2         21         18 일요일         103
27          0          4          2          9         21 일요일          91
28          0          6         15        154         67 일요일         425
29          0          7          0         66         49 일요일         228
30          0          4          6         69         49 토요일         227
31          0          4          0          4         72 토요일         151
32          0          4          3         71         30 토요일         195
33          0          3          0         13         50 토요일         122
34          0          3          5        103         55 토요일         260
35          0          3         12        150         54 토요일         377
36          0          0          1          4          6 토요일          54
37          0          2          3        123         30 화요일         203
38          0          4          2         35         30 화요일         147
39          0          3          7        112         47 화요일         305

 

[문제풀이]

# step1) 요일출력
act1$day <- strftime(as.Date(act1$date, '%Y-%m-%d'), '%A')

# step2) 요일별, 유저별 act_1_cnt 총합
library(plyr)

act1_2 <- ddply(act1, .(day, user_id), summarise, total_act1 = sum(act_1_cnt))

# step3) 요일별 act_1_cnt가 가장 많은 행 출력
userid <- ddply(act1_2, .(day), subset, total_act1 == max(total_act1))[, 'user_id']

# step4) 위 user_id에 대한 데이터만 추출
ncol(act1)
res2 <- sum(rowSums(act1[act1$user_id %in% userid, -c(1,2,20,21)]))
print(res2)

 

 

 

 

 2. 부동산_매매지수현황.csv 파일을 읽고

df1 <- read.csv('부동산_매매지수현황.csv', fileEncoding = 'cp949', skip=1)

 

더보기

[문제풀이 : 기초 데이터 처리]

# step1) 컬럼 이름 변경
library(stringr)
colnames(df1) <- str_sub(colnames(df1), 1, 2)
colnames(df1)[colnames(df1) == 'X.'] <- NA

v1 <- na.locf(colnames(df1))
colnames(df1) <- str_c(v1, df1[1,], sep='_')


# step2) 불필요한 행 제거, 숫자타입 변경
df1 <- df1[-c(1,2),]
df1[,-1] <- apply(df1[,-1], 2, as.numeric)

df1

 

> df1
           X_ 서울_활발함 서울_한산함 부산_활발함 부산_한산함 대구_활발함 대구_한산함 인천_활발함
3  2008-04-07         2.0        73.5         1.3        78.3         0.9        90.9         4.2
4  2008-04-14         2.1        73.1         0.8        75.1         1.1        89.5         2.4
5  2008-04-21         1.7        74.6         0.0        70.0         0.5        92.1         3.0
6  2008-04-28         1.0        80.2         0.0        73.6         0.0        94.6         1.9
7  2008-05-05         0.7        80.6         1.1        75.1         0.6        95.2         4.0
8  2008-05-12         0.7        84.6         0.0        79.9         0.0        93.7         1.5
9  2008-05-19         0.7        84.1         0.5        79.6         0.0        95.1         2.8
10 2008-05-26         0.7        85.6         0.9        83.6         0.0        96.3         4.2

 


 1) 각 지역별 활발함지수의 평균을 구하고 활발함지수가 높은 순서대로 3개지역의 활발함지수 평균 출력

더보기

[내 답안]

 

names(df1) <- str_remove_all(names(df1), '(^X.)|(\\.{2,}\\d*)')
names(df1) <- str_remove_all(names(df1), '\\.')

head(df1)

# 활발함지수 열만 선택
df2 <- df1[,df1[1,] ==  '활발함']
class(df2)
df3 <- df2[-c(1,2),]
...
df3[,7] <- as.numeric(df3[,7])

# 각 지역별 활발함지수 평균
활발함지수 <- apply(df3, 2, mean)

# 활발함지수 3개지역의 평균 출력
mean(rank(-활발함지수) <= 3)

 

> 활발함지수 <- apply(df3, 2, mean)
> 활발함지수
  서울Seoul   부산Busan   대구Daegu 인천Incheon 광주Gwangju 대전Daejeon   울산Ulsan 
  1.0463938   0.6725146   0.5374269   0.9140351   0.7436647   0.4387914   0.5959064 
> # 활발함지수 3개지역의 평균 출력
> mean(rank(-활발함지수) <= 3)
[1] 0.4285714

 

 

   => TRUE / FALSE에 대한 평균 출력이 되었으므로 0.4285...가 나왔다.

> rank(-활발함지수) <= 3
  서울Seoul   부산Busan   대구Daegu 인천Incheon 광주Gwangju 대전Daejeon   울산Ulsan 
       TRUE       FALSE       FALSE        TRUE        TRUE       FALSE       FALSE 
> # 활발함지수 3개지역의 평균 출력
> mean(rank(-활발함지수) <= 3)
[1] 0.4285714

 

mean(활발함지수[rank(-활발함지수) <= 3])를 통해 원래 값을 지정해주었어야 문제풀이와 동일한 결과가 나옴을 확인할 수 있다.

> mean(활발함지수[rank(-활발함지수) <= 3])
[1] 0.9013645

 

[문제풀이]

 

# 활발함 열만 선택
df1_1 <- df1[, str_detect(colnames(df1), '활발함')]
res3 <- mean(sort(colMeans(df1_1), decreasing = T)[1:3])

print(res3)

> sort(colMeans(df1_1), decreasing = T)[1:3]
서울_활발함 인천_활발함 광주_활발함 
  1.0463938   0.9140351   0.7436647 
> print(res3)
[1] 0.9013645

 2) 연도별로 각 지역의 한산함 지수의 평균을 구하고 이중 가장 큰값과 가장 작은값의 차이를 출력

더보기

[내 답안]

 

df1 <- read.csv('부동산_매매지수현황.csv', fileEncoding = 'cp949', skip=1)
df1
names(df1) <- str_remove_all(names(df1), '(^X.)|(\\.{2,}\\d*)')
names(df1) <- str_remove_all(names(df1), '\\.')

# 연도 추출
df1$X
as.Date(df1$X, '%Y-%m-%d')
df1$연도 <- str_sub(df1$X, 1, 4)
df1

# 이름전달
names(df1)

names(df1)[nchar(names(df1)) == 1] <- NA
head(df1)


names(df1)[1] <- 'date1'
names(df1) <- na.locf(names(df1))


# 한산함지수 열만 선택
df2 <- df1[, df1[1,] != '활발함']
head(df2)

# 연도별 각 지역 한산함 지수 평균 구하기
df2[, 2:8] <- lapply(df2[, 2:8], as.numeric)
head(df2)
df3 <- df2[-c(1,2),-1]

df4 <- aggregate(. ~ 연도, data = df3, mean, na.rm = T)
df4
#이중 가장 큰값과 가장 작은값의 차이를 출력
max(df4[,-1]) - min(df4[,-1])

 

> df4
   연도 서울Seoul 부산Busan 대구Daegu 인천Incheon 광주Gwangju 대전Daejeon 울산Ulsan
1  2008  92.66154  86.50000  96.19744    80.91538    93.85641    87.82308  93.41538
2  2009  87.82549  77.90980  88.39412    90.20784    93.64510    80.64706  81.21176
3  2010  95.21765  78.03137  88.01569    95.89020    88.88627    83.79608  82.15882
4  2011  92.26600  83.85600  73.27800    93.43200    85.56600    86.18600  76.72000
5  2012  97.02745  94.92745  80.82549    96.44314    89.30588    95.06275  88.72549
6  2013  91.02353  89.22549  78.82157    84.31176    91.37843    87.49020  88.48431
7  2014  81.76735  81.44694  78.17143    77.40612    84.00816    84.71837  75.62653
8  2015  66.89800  73.01200  80.28200    66.92400    87.43800    82.97000  69.75800
9  2016  75.76600  76.90800  93.87200    73.91400    89.43800    80.16600  91.73600
10 2017  79.70800  92.64600  85.09400    82.18600    90.30600    86.18200  94.76400
11 2018  81.15714  97.00000  90.34762    89.38095    88.35714    85.00476  96.67619
> #이중 가장 큰값과 가장 작은값의 차이를 출력
> max(df4[,-1]) - min(df4[,-1])
[1] 30.12945

 

[문제풀이]

 

# step1) 한산함 데이터만 추출
df1_2 <- df1[, str_detect(colnames(df1), '한산함')]

# step2) 연도 추출
df1_2$year <- str_sub(df1$X_, 1, 4)

# step3) 연도별로 각 지역의 한산함지수 평균
library(plyr)

# 3-1) ddply 사용
ddply(df1_2, .(year), summarise, 서울평균 = mean(서울_한산함),
                                 부산평균 = mean(부산_한산함),
                                 대구평균 = mean(대구_한산함),
                                 인천평균 = mean(인천_한산함),
                                 광주평균 = mean(광주_한산함),
                                 대전평균 = mean(대전_한산함),
                                 울산평균 = mean(울산_한산함))

# 3-2) aggregate 사용
aggregate(df1_2[,-8], list(df1_2$year), mean)


total <- aggregate(df1_2[,-8], list(df1_2$year), mean)
res4 <- max(total[,-1]) - min(total[,-1])
print(res4)

> print(res4)
[1] 30.12945

 3) 서울 지역의 월별 활발함 지수와 한산함 지수를 구하고 
    각각 가장 높은 월의 활발함 지수와 한산함 지수의 총 합을 출력

더보기

[내 답안]

 

head(df1)


df5 <- df1[1:3]
df5$month <- str_sub(df5$X, 6,7)

head(df5)  
names(df5)[2] <- '활발함'
names(df5)[3] <- '한산함'

df5 <- df5[-c(1,2),-1]

df5[,1] <- as.numeric(df5[,1])
df5[,2] <- as.numeric(df5[,2])

str(df5)

df6 <- aggregate(. ~ month, data = df5, mean, na.rm = T)
df6

max(df6$활발함) + max(df6$한산함)

 

> df6 <- aggregate(. ~ month, data = df5, mean, na.rm = T)
> df6
   month    활발함   한산함
1     01 0.8121951 88.28537
2     02 1.0189189 84.87297
3     03 1.4704545 82.60000
4     04 1.0212766 85.11064
5     05 1.2934783 85.30000
6     06 1.6090909 83.86136
7     07 1.4976744 84.83488
8     08 0.8666667 85.29556
9     09 1.1648649 82.00270
10    10 0.9714286 83.83333
11    11 0.4302326 89.92791
12    12 0.3863636 92.40682
> max(df6$활발함) + max(df6$한산함)
[1] 94.01591

 

[문제풀이]

 

# step1) 서울지역 추출
df1_3 <- df1[,c(2,3)]

# step2) 월 추출
df1_3$month <- str_sub(df1$X, 6, 7)

# step3) 월별 활발함 / 한산함 평균
total2 <- ddply(df1_3, .(month), summarise, 활발함평균 = mean(서울_활발함), 한산함평균 = mean(서울_한산함))

# step4) 각 최댓값 확인
res5 <- max(total2$활발함평균) + max(total2$한산함평균)

print(res5)

> print(res5)
[1] 94.01591