본문 바로가기

■ 프로그래밍/R

[R 기초] 여러 데이터 한 번에 불러오기

[ R : 여러 데이터 한 번에 불러오기 ]


패키지 설치 및 로딩하기

install.packages("readxl")
install.packages("data.table")
install.packages("ggplot2")
library(readxl)
library(data.table)
library(ggplot2)


폴더 안 파일 보기

dir() # 현재 폴더에 뭐가 있는지
file.list <- dir(pattern = "xlsx")    # 폴더안에서 ".확장자"만 보이기
data.list <- lapply(file.list, 
                    read_excel,
                    sheet = 1,
                    col_names = TRUE) # 변수명을 그대로 읽어옴
data.list
## [[1]]
## # A tibble: 20 x 4
##       id height weight    iq
##    <dbl>  <dbl>  <dbl> <dbl>
##  1     1    174     73   120
##  2     2    165     50   140
##  3     3    168     50   150
##  4     4    169     50   140
##  5     5    180     70   180
##  6     6    160     48   136
##  7     7    188     73   160
##  8     8    190     90    90
##  9     9    183     63    95
## 10    10    180     70   120
## 11    11    164     58   200
## 12    12    157     45   130
## 13    13    160     50   100
## 14    14    161     48   120
## 15    15    179     72    80
## 16    16    160     50   130
## 17    17    170     70   130
## 18    18    165     50   130
## 19    19    178     75   100
## 20    20    165     48   155


데이터 중에서 numeric 데이터만 추출하기

ggplot2::diamonds 데이터를 사용하겠다.

str(diamonds) # 데이터 structure보기, 데이터와 변수들의 유형을 알 수 있다
## Classes 'tbl_df', 'tbl' and 'data.frame':    53940 obs. of  10 variables:
##  $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##  $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##  $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##  $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...

sapply(data, function)

sapply(diamonds, is.numeric) # diamonds에서 numeric인 것을 알려달라
##   carat     cut   color clarity   depth   table   price       x       y 
##    TRUE   FALSE   FALSE   FALSE    TRUE    TRUE    TRUE    TRUE    TRUE 
##       z 
##    TRUE

data[ , 위의 함수]로 해당하는 열의 데이터를 볼 수 도 있다.

diamonds[ , sapply(diamonds, is.numeric)]
## # A tibble: 53,940 x 7
##    carat depth table price     x     y     z
##    <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
##  1 0.23   61.5    55   326  3.95  3.98  2.43
##  2 0.21   59.8    61   326  3.89  3.84  2.31
##  3 0.23   56.9    65   327  4.05  4.07  2.31
##  4 0.290  62.4    58   334  4.2   4.23  2.63
##  5 0.31   63.3    58   335  4.34  4.35  2.75
##  6 0.24   62.8    57   336  3.94  3.96  2.48
##  7 0.24   62.3    57   336  3.95  3.98  2.47
##  8 0.26   61.9    55   337  4.07  4.11  2.53
##  9 0.22   65.1    61   337  3.87  3.78  2.49
## 10 0.23   59.4    61   338  4     4.05  2.39
## # ... with 53,930 more rows


apply(matreix or data.frame, margin = 1 or 2, function)

  • 행렬 구조로 있을 때 유용
  • margin = 1 : 행, 2 : 열
col <- apply(diamonds[ , c(1, 5:10)], 1, mean) # 각 행에 대한 평균
head(col)
## [1] 64.72714 65.29286 65.65143 66.53571 66.86429 66.63143
row <- apply(diamonds[ , c(1, 5:10)], 2, mean) # 각 열에 대한 평균
head(row)
##        carat        depth        table        price            x 
##    0.7979397   61.7494049   57.4571839 3932.7997219    5.7311572 
##            y 
##    5.7345260