이번 포스팅에서는 장비별로 On, Off 상태 변화 간의 가동 시간(run time)을 구하고, 이렇게 구한 장비별 가동 시간의 평균을 집계해보겠습니다. 




먼저, 장비(device), 날짜/시간(time), status('ON', 'OFF') 의 3개 변수로 구성된 간단한 예제 DataFrame을 만들어보겠습니다. 



> #===========================

> # time difference by device

> #===========================

> device <- c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C')

> time <- c('2018-07-01 10:20:23', '2018-07-01 10:30:55', '2018-07-01 11:11:01',

+           '2018-07-01 11:51:41', '2018-07-02 07:11:02', '2018-07-02 09:00:33',

+           '2018-07-02 09:20:24', '2018-07-02 12:12:21', '2018-07-02 14:01:09',

+           '2018-07-02 18:11:41', '2018-07-02 19:21:51', '2018-07-02 20:30:00')

> status <- c('ON', 'ON', 'ON', 'OFF', 'ON', 'ON', 'OFF', 'ON', 'OFF', 'ON', 'ON', 'OFF')

> df <- data.frame(device, time, status)


> df

   device                time status

1       A 2018-07-01 10:20:23     ON   <-- start

2       A 2018-07-01 10:30:55     ON

3       A 2018-07-01 11:11:01     ON

4       A 2018-07-01 11:51:41    OFF    <-- end


5       B 2018-07-02 07:11:02     ON    <-- start

6       B 2018-07-02 09:00:33     ON

7       B 2018-07-02 09:20:24    OFF    <-- end


8       C 2018-07-02 12:12:21     ON    <-- start

9       C 2018-07-02 14:01:09    OFF     <-- end

10      C 2018-07-02 18:11:41     ON    <-- start

11      C 2018-07-02 19:21:51     ON

12      C 2018-07-02 20:30:00    OFF     <-- end




다음으로 장비(device)와 날짜/시간(time)을 기준으로 정렬(sort)을 하겠습니다. 



> # sort df by device and time in ascending order

> df <- df[order(device, time),]

> df

   device                time status

1       A 2018-07-01 10:20:23     ON

2       A 2018-07-01 10:30:55     ON

3       A 2018-07-01 11:11:01     ON

4       A 2018-07-01 11:51:41    OFF

5       B 2018-07-02 07:11:02     ON

6       B 2018-07-02 09:00:33     ON

7       B 2018-07-02 09:20:24    OFF

8       C 2018-07-02 12:12:21     ON

9       C 2018-07-02 14:01:09    OFF

10      C 2018-07-02 18:11:41     ON

11      C 2018-07-02 19:21:51     ON

12      C 2018-07-02 20:30:00    OFF




각 장비(device) 별로 'ON' 이후에 다음 'OFF' 까지의 사이 중간에 끼어있는 'ON'은 필요가 없으므로 삭제를 해서 새로운 'df_2' 이름의 DataFrame을 만들어보겠습니다. 


[ 전처리 후의 Output Image ]


   device                time status

1       A 2018-07-01 10:20:23     ON   <-- start

2       A 2018-07-01 10:30:55     ON

3       A 2018-07-01 11:11:01     ON

4       A 2018-07-01 11:51:41    OFF    <-- end


5       B 2018-07-02 07:11:02     ON    <-- start

6       B 2018-07-02 09:00:33     ON

7       B 2018-07-02 09:20:24    OFF    <-- end


8       C 2018-07-02 12:12:21     ON    <-- start

9       C 2018-07-02 14:01:09    OFF     <-- end

10      C 2018-07-02 18:11:41     ON    <-- start

11      C 2018-07-02 19:21:51     ON

12      C 2018-07-02 20:30:00    OFF     <-- end

 





> # set of device

> device_set <- as.character(unique(df$device))

> # blank DataFrame to store the preprocessed dataset

> df_2 <- data.frame()

> for (i in 1:length(device_set)){

+   # split dataframe by device

+   device_i <- device_set[i]

+   df_i <- df[df$device == device_i, ]

+   

+   # add the first time of df_i to df2

+   df_2 <- rbind(df_2, df_i[1,])

+   

+   # add the time if the device is turned off or turned on again

+   for (j in 1:(nrow(df_i)-1)){

+     if((df_i$status[j] == 'ON' & df_i$status[j+1] == 'OFF') | 

+        (df_i$status[j] == 'OFF' & df_i$status[j+1] == 'ON')){

+       df_2 <- rbind(df_2, df_i[j+1,])

+     }

+   }

+ }

> df_2

   device                time status

1       A 2018-07-01 10:20:23     ON

4       A 2018-07-01 11:51:41    OFF

5       B 2018-07-02 07:11:02     ON

7       B 2018-07-02 09:20:24    OFF

8       C 2018-07-02 12:12:21     ON

9       C 2018-07-02 14:01:09    OFF

10      C 2018-07-02 18:11:41     ON

12      C 2018-07-02 20:30:00    OFF





lag() window function을 사용해서 장비, 날짜/시간, 상태를 한칸씩 밑으로 내리고(Lag), 장비 이름과 상태를 기준으로 필요한 행만 남기고 나머지는 삭제하겠습니다. 



> # lag window function

> library(dplyr)

> df_2_lag <- mutate(df_2, 

+                    device_lag = lag(device, 1), 

+                    time_lag = lag(time, 1), 

+                    status_lag = lag(status, 1))

> # filtering

> df_2_lag_filtered <- df_2_lag %>% 

 filter(device == device_lag & status == 'OFF')

> df_2_lag_filtered

  device                time status device_lag            time_lag status_lag

1      A 2018-07-01 11:51:41    OFF          A 2018-07-01 10:20:23         ON

2      B 2018-07-02 09:20:24    OFF          B 2018-07-02 07:11:02         ON

3      C 2018-07-02 14:01:09    OFF          C 2018-07-02 12:12:21         ON

4      C 2018-07-02 20:30:00    OFF          C 2018-07-02 18:11:41         ON




이제 장비(device)별로 'ON' 이후 다음번 'OFF' 까지의 가동 시간(run_time)을 difftime() 함수를 사용해서 구해보겠습니다. 이때 strptime() 함수를 사용해서 문자열 값을 '년/월/일/시간/분/초("%Y-%m-%d %H:%M:%S")' 형태로 만들어준 후에 difftime() 함수를 사용할 수 있습니다.  이렇 구한 가동시간은 '시간(hour)' 단위 값입니다. (분으로 환산하려면 곱하기 60, 초로 환산하려면 곱하기 60*60 을 해주면 됩니다) 



> # calculate the run_time

> df_2_lag_filtered$run_time <- as.numeric(difftime(strptime(df_2_lag_filtered$time, "%Y-%m-%d %H:%M:%S"),

+                                                   strptime(df_2_lag_filtered$time_lag, "%Y-%m-%d %H:%M:%S")))

> df_2_lag_filtered

  device                time status device_lag            time_lag status_lag run_time

1      A 2018-07-01 11:51:41    OFF          A 2018-07-01 10:20:23         ON 1.521667

2      B 2018-07-02 09:20:24    OFF          B 2018-07-02 07:11:02         ON 2.156111

3      C 2018-07-02 14:01:09    OFF          C 2018-07-02 12:12:21         ON 1.813333

4      C 2018-07-02 20:30:00    OFF          C 2018-07-02 18:11:41         ON 2.305278

 




마지막으로, 각 장비(device)별 가동시간(run_time)의 평균을 구해보겠습니다. 



> # average of run_time by device

> run_time_by_device <- df_2_lag_filtered %>% 

+   group_by(device) %>% 

+   summarise(run_time_avg = mean(run_time))

> run_time_by_device

# A tibble: 3 x 2

  device run_time_avg

  <fct>         <dbl>

1 A              1.52

2 B              2.16

3 C              2.06




많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요.



728x90
반응형
Posted by Rfriend
,

R의 다양한 패키지들을 사용하다 보면 함수를 사용한 후의 산출물 결과가 자신이 생각했던 결과와 다를 경우 R 패키지 함수의 소스 코드가 어떻게 되어있는 것인지 궁금할 때가 있습니다. 


이번 포스팅에서는 R 패키지에 있는 함수, 메소드의 소스 코드를 들여다볼 수 있는 2가지 방법을 소개하겠습니다. 


(1) getAnywhere(function_name)

(2) package_name:::function_name





가령, 두 벡터의 차집합(difference of sets)을 구할 때 사용하는 함수가 base 패키지의 setdiff() 함수입니다. 


> x <- c(1, 1, 2, 3, 4, 5)

> y <- c(4, 5, 6, 7)

> setdiff(x, y)

[1] 1 2 3




위에서 예로 든 base 패키지의 setdiff() 함수에 대한 소스 코드를 위의 2가지 방법을 사용해서 살펴보겠습니다. 



 (1) getAnywhere(function_name)


R package 의 이름을 몰라도 함수, 메소드의 이름만 알고 있으면 R package 이름과 namespace, 그리고 소스 코드를 친절하게 다 볼 수 있으므로 매우 편리합니다. 



> getAnywhere(setdiff)

A single object matching ‘setdiff’ was found

It was found in the following places

  package:base

  namespace:base

with value


function (x, y) 

{

    x <- as.vector(x)

    y <- as.vector(y)

    unique(if (length(x) || length(y)) 

        x[match(x, y, 0L) == 0L]

    else x)

}

<bytecode: 0x000000000ff1e8d0>

<environment: namespace:base>

 




 (2) R_package_name:::function_name


R package 이름을 알고 있다면 package_name:::function_name 의 형식으로 ':::' 를 사용해서 함수의 소스 코드를 들여다볼 수 있습니다. 위 (1)번의 getAnywhere() 함수가 기억이 잘 안날 때 쉽게 ':::' 를 사용해서 소스 코드 볼 때 편리합니다. 



> base:::setdiff

function (x, y) 

{

    x <- as.vector(x)

    y <- as.vector(y)

    unique(if (length(x) || length(y)) 

        x[match(x, y, 0L) == 0L]

    else x)

}

<bytecode: 0x000000000ff1e8d0>

<environment: namespace:base>




많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 (1) 시계열 정수의 순차 3개 묶음 패턴 별 개수를 구하고, (2) 각 패턴별로 발생 빈도 기준으로 내림차순 정렬하는 방법을 소개하겠습니다. 



먼저 S = {1, 2, 3, 4, 5} 의 정수 집합에서 복원추출(replacement) 하여 무작위 샘플 1,000개를 생성해보겠습니다. 



> #----------------------------------

> # counting and sorting by patterns

> #----------------------------------

> set.seed(123) # for reproducibility

> sample <- sample(x=1:5, size=1000, replace=TRUE)

> sample

   [1] 2 4 3 5 5 1 3 5 3 3 5 3 4 3 1 5 2 1 2 5 5 4 4 5 4 4 3 3 2 1 5 5 4 4 1 3 4 2 2 2 1 3 3 2 1 1 2

  [48] 3 2 5 1 3 4 1 3 2 1 4 5 2 4 1 2 2 5 3 5 5 4 3 4 4 4 1 3 2 2 4 2 1 2 4 3 4 1 3 5 5 5 1 1 4 2 4

  [95] 2 1 4 1 3 3 3 2 3 5 3 5 5 4 3 1 5 2 1 5 4 1 3 5 3 3 4 2 2 2 2 5 1 1 1 4 4 5 4 4 3 4 5 4 5 3 2

 [142] 3 1 1 5 2 2 1 2 4 5 3 2 2 1 2 3 2 3 2 3 2 4 2 2 3 4 2 3 2 4 1 5 4 4 4 2 3 5 3 5 2 4 2 3 3 2 3

 [189] 5 5 2 2 5 4 5 3 3 4 1 3 2 5 4 3 3 5 2 2 1 1 3 2 2 4 1 4 2 3 5 5 2 5 4 4 1 2 3 3 4 5 4 3 3 1 2

 [236] 2 1 5 1 5 3 4 1 4 2 4 2 5 5 4 2 2 3 2 3 4 1 3 3 5 5 5 4 5 3 3 2 2 1 3 5 1 1 1 4 4 5 3 1 4 4 1

 [283] 2 2 1 2 1 2 1 4 2 1 1 5 4 5 5 1 1 4 4 1 4 4 4 3 1 1 3 3 2 3 4 1 2 5 5 2 2 5 5 2 1 4 1 1 5 1 2

 [330] 5 4 2 4 5 2 3 4 4 4 5 3 1 3 2 3 2 5 2 2 4 1 5 3 1 4 5 4 3 2 5 1 1 5 2 2 4 1 1 3 3 3 2 5 3 3 5

 [377] 4 2 2 5 3 4 2 4 3 5 1 2 1 2 5 2 4 4 1 2 2 1 1 5 5 1 5 3 2 3 4 1 2 4 2 5 2 3 2 1 5 2 3 1 3 4 3

 [424] 2 5 4 2 2 1 5 5 3 2 5 2 4 1 3 1 1 2 3 4 1 5 4 4 1 2 4 3 2 2 1 2 5 5 4 1 1 5 3 3 1 3 2 2 4 2 5

 [471] 1 5 3 4 4 3 4 1 2 4 2 5 2 4 5 1 1 4 3 5 5 1 3 4 1 5 2 1 4 4 2 2 2 1 2 1 3 3 5 2 3 1 5 2 4 1 3

 [518] 4 1 5 2 2 2 1 3 4 5 1 5 2 5 4 3 5 1 4 2 5 3 2 5 1 5 2 4 5 1 5 5 2 2 2 3 5 3 3 4 3 3 1 1 5 4 2

 [565] 4 4 2 5 3 5 3 5 3 3 5 4 3 1 3 3 5 5 5 2 5 1 2 4 5 3 3 1 5 1 1 5 3 2 5 1 2 4 2 2 1 5 1 5 2 2 4

 [612] 1 1 5 3 4 5 4 5 1 5 5 4 2 1 2 2 5 2 2 4 5 3 4 1 2 5 4 5 3 3 4 5 2 1 4 4 1 2 4 4 5 3 3 4 5 5 1

 [659] 3 2 5 5 1 4 2 1 5 1 4 1 1 3 1 2 3 3 5 2 3 3 4 4 5 5 5 3 3 1 3 2 1 4 3 1 2 3 1 1 3 2 5 2 1 5 2

 [706] 4 3 2 1 3 4 3 4 4 3 2 1 1 5 1 3 4 4 5 4 4 5 4 4 1 3 5 4 5 1 2 5 5 2 1 5 1 1 3 2 3 4 1 4 5 2 2

 [753] 1 1 3 3 3 5 1 2 3 2 3 3 3 1 1 3 4 4 5 5 4 5 1 2 2 3 4 5 2 5 1 4 5 3 2 1 2 5 5 3 2 3 5 3 1 2 2

 [800] 2 3 2 1 1 2 5 3 3 4 4 1 4 2 5 4 1 2 1 3 3 4 2 4 5 4 1 3 1 2 4 1 4 3 4 4 3 4 4 2 1 5 4 4 1 3 2

 [847] 5 1 2 1 2 4 5 4 3 3 5 1 1 5 3 1 4 3 4 2 1 1 3 3 1 1 4 3 5 4 1 4 5 4 1 5 4 1 4 5 3 2 2 2 3 4 4

 [894] 3 5 4 1 2 1 4 5 3 5 3 4 3 4 5 5 1 1 1 5 3 2 5 3 3 2 2 5 1 2 5 4 4 2 3 1 4 5 2 4 3 5 1 3 5 5 1

 [941] 5 3 2 3 1 1 3 2 3 2 3 1 5 1 2 5 4 1 3 3 3 3 4 1 5 3 3 1 4 3 4 1 5 2 2 4 5 1 2 3 3 4 5 4 5 4 1

 [988] 1 1 3 1 2 3 3 3 5 4 2 4 1

 




다음으로 순차적으로 3개의 정수를 묶음으로 하여 1개씩 이동하면서 패턴을 정의하고(패턴1 {X1, X2, X3}, 패턴2 {X2, X3, X4}, ..., 패턴n-2 {Xn-2, Xn-1, X}, 위의 정수 난수 샘플을 가지고 예를 들면, 첫번째 3개 묶음의 패턴은 '2_4_3', 두번째 3개 묶음의 패턴은 '4_3_5', 세번째 3개 묶음의 패턴은 '3_5_5', ... ), 각 패턴별로 발생 빈도를 세어보겠습니다


먼저 비어있는 pattern_list 라는 이름의 리스트(list) 를 만들어놓구요, 


for loop 반복문을 사용하여 '패턴1' {X1, X2, X3}, '패턴2' {X2, X3, X4}, ..., '패턴n-2' {Xn-2, Xn-1, X} 을 생성합니다. 


if else 조건문을 사용하여, 만약 3개 정수 묶음의 패턴이 pattern_list의 키(key)에 이미 들어있는 것이면 +1 을 추가해주구요, 그렇지 않다면 (처음 발생하는 패턴이라면) pattern_list의 키(key)에 새로 발생한 패턴을 키로 추가해주고 값(value)으로 1을 부여해줍니다. 



> # blank list to store the patterns' count

> pattern_list <- {}

> # count per patterns

> for (i in 1:(length(sample)-2)){

+   

+   pattern <- paste(sample[i], sample[i+1], sample[i+2], sep="_")

+   

+   if (pattern %in% names(pattern_list)) {

+     pattern_list[[pattern]] <- pattern_list[[pattern]] + 1

+   } else {

+     pattern_list[[pattern]] <- 1

+   }

+ }

> pattern_list

2_4_3 4_3_5 3_5_5 5_5_1 5_1_3 1_3_5 3_5_3 5_3_3 3_3_5 5_3_4 3_4_3 4_3_1 3_1_5 1_5_2 5_2_1 2_1_2 

    6     7    10     9     6     6    10    15    11     7     5     5     5    13     7    12 

1_2_5 2_5_5 5_5_4 5_4_4 4_4_5 4_5_4 4_4_3 4_3_3 3_3_2 3_2_1 2_1_5 1_5_5 4_4_1 4_1_3 1_3_4 3_4_2 

   11     8     9    11     9    13     7     5     8     9    10     5    11    14     9     6 

4_2_2 2_2_2 2_2_1 2_1_3 1_3_3 2_1_1 1_1_2 1_2_3 2_3_2 3_2_5 2_5_1 3_4_1 1_3_2 2_1_4 1_4_5 4_5_2 

    8     8    14     6    11     8     3     9    11    13    10    15    12     8     8     6 

5_2_4 2_4_1 4_1_2 1_2_2 2_2_5 2_5_3 5_3_5 5_4_3 4_3_4 3_4_4 4_4_4 3_2_2 2_2_4 2_4_2 4_2_1 1_2_4 

    9    10    12     7     7     7     6     8    10    11     4     8     8     9     7    10 

5_5_5 5_1_1 1_1_4 1_4_2 4_2_4 1_4_1 3_3_3 3_2_3 2_3_5 1_5_4 5_4_1 3_3_4 1_1_1 1_4_4 3_4_5 5_4_5 

    4    10     6     7     7     3     7    15     6     7    10    11     4     7     9     9 

4_5_3 5_3_2 2_3_1 3_1_1 1_1_5 5_2_2 2_4_5 3_2_4 2_2_3 2_3_4 4_2_3 4_1_5 4_4_2 3_5_2 2_3_3 5_5_2 

   13    11     7     8    12    12     9     2     6     9     5     8     5     4     7     7 

2_5_4 1_1_3 4_1_4 5_2_5 3_3_1 3_1_2 1_5_1 5_1_5 1_5_3 4_2_5 5_4_2 3_5_1 5_3_1 3_1_4 1_2_1 4_5_5 

   10    11     8     4     8     6     8     9    11     7     7     6     5     5     7     5 

4_1_1 5_1_2 5_2_3 3_1_3 2_5_2 4_3_2 3_5_4 2_4_4 5_5_3 1_3_1 4_5_1 1_4_3 5_1_4 

    6    11     5     5     7     5     6     3     3     4     7     6     4 

 




이제 발생 빈도를 기준으로 패턴 리스트를 내림차순 정렬(sort in descending order)을 해보겠습니다. 

'5_3_3', '3_4_1', '3_2-3' 패턴이 총 15번 발생해서 공동 1등을 하였네요. 



> # sorting pattern_list in descending order

> sort(pattern_list, decreasing = TRUE)

5_3_3  3_4_1  3_2_3  4_1_3 2_2_1 1_5_2 4_5_4 3_2_5 4_5_3 2_1_2 1_3_2 4_1_2 1_1_5 5_2_2 3_3_5 1_2_5 

   15     15      15     14    14    13    13    13    13    12    12    12    12    12    11    11 

5_4_4 4_4_1 1_3_3 2_3_2 3_4_4 3_3_4 5_3_2 1_1_3 1_5_3 5_1_2 3_5_5 3_5_3 2_1_5 2_5_1 2_4_1 4_3_4 

   11    11    11    11    11    11    11    11    11    11    10    10    10    10    10    10 

1_2_4 5_1_1 5_4_1 2_5_4 5_5_1 5_5_4 4_4_5 3_2_1 1_3_4 1_2_3 5_2_4 2_4_2 3_4_5 5_4_5 2_4_5 2_3_4 

   10    10    10    10     9     9     9     9     9     9     9     9     9     9     9     9 

5_1_5 2_5_5 3_3_2 4_2_2 2_2_2 2_1_1 2_1_4 1_4_5 5_4_3 3_2_2 2_2_4 3_1_1 4_1_5 4_1_4 3_3_1 1_5_1 

    9     8     8     8     8     8     8     8     8     8     8     8     8     8     8     8 

4_3_5 5_3_4 5_2_1 4_4_3 1_2_2 2_2_5 2_5_3 4_2_1 1_4_2 4_2_4 3_3_3 1_5_4 1_4_4 2_3_1 2_3_3 5_5_2 

    7     7     7     7     7     7     7     7     7     7     7     7     7     7     7     7 

4_2_5 5_4_2 1_2_1 2_5_2 4_5_1 2_4_3 5_1_3 1_3_5 3_4_2 2_1_3 4_5_2 5_3_5 1_1_4 2_3_5 2_2_3 3_1_2 

    7     7     7     7     7     6     6     6     6     6     6     6     6     6     6     6 

3_5_1 4_1_1 3_5_4 1_4_3 3_4_3 4_3_1 3_1_5 4_3_3 1_5_5 4_2_3 4_4_2 5_3_1 3_1_4 4_5_5 5_2_3 3_1_3 

    6     6     6     6     5     5     5     5     5     5     5     5     5     5     5     5 

4_3_2 4_4_4 5_5_5 1_1_1 3_5_2 5_2_5 1_3_1 5_1_4 1_1_2 1_4_1 2_4_4 5_5_3 3_2_4 

    5     4     4     4     4     4     4     4     3     3     3     3     2

 




위의 패턴별 발생 빈도수 기준으로 정렬된 결과를 DataFrame으로 변환해서 상위 10개 패턴을 프린트해보겠습니다. 



> # convert a list to DataFrame

> cnt_per_pattern_sorted <- sort(pattern_list, decreasing = TRUE)

> pattern <- names(cnt_per_pattern_sorted)

> df <- data.frame(pattern, cnt_per_pattern_sorted)

> rownames(df) <- NULL

> # display top 10 patterns

> df[1:10,]

   pattern     cnt_per_pattern_sorted

1    5_3_3                     15

2    3_4_1                     15

3    3_2_3                     15

4    4_1_3                     14

5    2_2_1                     14

6    1_5_2                     13

7    4_5_4                     13

8    3_2_5                     13

9    4_5_3                     13

10   2_1_2                     12

 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. ^^



728x90
반응형
Posted by Rfriend
,

회사의 보안 정책 상 사내 폐쇄망으로 운영함에 따라 R 패키지를 인터넷으로부터 바로 다운로드하지 못하는 경우가 있습니다. 이처럼 폐쇄망일 경우 R 패키지를 설치하는 것이 '지옥 그 자체!' 일 수 있습니다. 왜냐하면 특정 R 패키지를 설치하려면 그와 의존성이 있는 다른 패키지를 설치해야만 하고, 그 의존성이 있는 패키지는 또 다른 의존성이 있는 패키지가 설치되어야만 하고.... 꼬리에 꼬리를 무는 의존성 있는 패키지들을 설치하다보면 입에서 투덜거림이 궁시렁 궁시렁 나오게 됩니다. -_-;;; 

 

이럴 때 편리하게 사용할 수 있는 'R 패키지 다운로드 & 의존성 있는 R 패키지도 같이 한꺼번에 다운로드 하기' 하는 방법을 소개하겠습니다. 

 

 

먼저 R 패키지를 다운로드 받아 놓을 'r-pkt' 폴더를 만들어보겠습니다. 

> mainDir <- "/Users/ihongdon/Downloads"

> subDir <- "r-pkg"

dir.create(file.path(mainDir, subDir), showWarnings = FALSE)

 

다음으로 tools 패키지의 package_dependencies() 함수를 이용하여 다운도르하려는 특정 R 패키지와 이 패키지의 의존성 있는 패키지(dependencies of package)들 정보를 가져오는 사용자 정의 함수 getPackages() 를 만들어보겠습니다. 

# UDF for get packages with dependencies

getPackages <- function(packs){

  packages <- unlist(

    # Find (recursively) dependencies or reverse dependencies of packages.

    tools::package_dependencies(packs, available.packages(),

                         which=c("Depends", "Imports"), recursive=TRUE)

  )

  packages <- union(packs, packages)

  

  return(packages)

}

 

 

자, 이제 준비가 되었으니 예제로 "dplyr"와 "ggplot2" 의 두 개 패키지와 이들과 의존성을 가지는 패키지들을 getPackages() 사용자 정의 함수로 정보를 가져온 후에, download.packages() 함수로 '/Users/ihongdon/Downloads/r-pkg' 폴더로 다운로드 해보도록 하겠습니다. 

> packages <- getPackages(c("dplyr", "ggplot2"))

download.packages(packages, destdir=file.path(mainDir, subDir))

 

trying URL 'https://cran.rstudio.com/src/contrib/dplyr_0.8.0.1.tar.gz'

Content type 'application/x-gzip' length 1075146 bytes (1.0 MB)

==================================================

downloaded 1.0 MB

trying URL 'https://cran.rstudio.com/src/contrib/ggplot2_3.1.1.tar.gz'

Content type 'application/x-gzip' length 2862022 bytes (2.7 MB)

==================================================

downloaded 2.7 MB

trying URL 'https://cran.rstudio.com/src/contrib/assertthat_0.2.1.tar.gz'

Content type 'application/x-gzip' length 12742 bytes (12 KB)

==================================================

downloaded 12 KB

trying URL 'https://cran.rstudio.com/src/contrib/glue_1.3.1.tar.gz'

Content type 'application/x-gzip' length 122950 bytes (120 KB)

==================================================

downloaded 120 KB

trying URL 'https://cran.rstudio.com/src/contrib/magrittr_1.5.tar.gz'

Content type 'application/x-gzip' length 200504 bytes (195 KB)

==================================================

downloaded 195 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'methods' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/pkgconfig_2.0.2.tar.gz'

Content type 'application/x-gzip' length 6024 bytes

==================================================

downloaded 6024 bytes

trying URL 'https://cran.rstudio.com/src/contrib/R6_2.4.0.tar.gz'

Content type 'application/x-gzip' length 31545 bytes (30 KB)

==================================================

downloaded 30 KB

trying URL 'https://cran.rstudio.com/src/contrib/Rcpp_1.0.1.tar.gz'

Content type 'application/x-gzip' length 3661123 bytes (3.5 MB)

==================================================

downloaded 3.5 MB

trying URL 'https://cran.rstudio.com/src/contrib/rlang_0.3.4.tar.gz'

Content type 'application/x-gzip' length 858992 bytes (838 KB)

==================================================

downloaded 838 KB

trying URL 'https://cran.rstudio.com/src/contrib/tibble_2.1.1.tar.gz'

Content type 'application/x-gzip' length 311836 bytes (304 KB)

==================================================

downloaded 304 KB

trying URL 'https://cran.rstudio.com/src/contrib/tidyselect_0.2.5.tar.gz'

Content type 'application/x-gzip' length 21883 bytes (21 KB)

==================================================

downloaded 21 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'utils' at the repositories

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'tools' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/cli_1.1.0.tar.gz'

Content type 'application/x-gzip' length 40232 bytes (39 KB)

==================================================

downloaded 39 KB

trying URL 'https://cran.rstudio.com/src/contrib/crayon_1.3.4.tar.gz'

Content type 'application/x-gzip' length 658694 bytes (643 KB)

==================================================

downloaded 643 KB

trying URL 'https://cran.rstudio.com/src/contrib/fansi_0.4.0.tar.gz'

Content type 'application/x-gzip' length 266123 bytes (259 KB)

==================================================

downloaded 259 KB

trying URL 'https://cran.rstudio.com/src/contrib/pillar_1.3.1.tar.gz'

Content type 'application/x-gzip' length 103972 bytes (101 KB)

==================================================

downloaded 101 KB

trying URL 'https://cran.rstudio.com/src/contrib/purrr_0.3.2.tar.gz'

Content type 'application/x-gzip' length 373701 bytes (364 KB)

==================================================

downloaded 364 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'grDevices' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/utf8_1.1.4.tar.gz'

Content type 'application/x-gzip' length 218882 bytes (213 KB)

==================================================

downloaded 213 KB

trying URL 'https://cran.rstudio.com/src/contrib/digest_0.6.18.tar.gz'

Content type 'application/x-gzip' length 128553 bytes (125 KB)

==================================================

downloaded 125 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'grid' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/gtable_0.3.0.tar.gz'

Content type 'application/x-gzip' length 368081 bytes (359 KB)

==================================================

downloaded 359 KB

trying URL 'https://cran.rstudio.com/src/contrib/lazyeval_0.2.2.tar.gz'

Content type 'application/x-gzip' length 83482 bytes (81 KB)

==================================================

downloaded 81 KB

trying URL 'https://cran.rstudio.com/src/contrib/MASS_7.3-51.4.tar.gz'

Content type 'application/x-gzip' length 487233 bytes (475 KB)

==================================================

downloaded 475 KB

trying URL 'https://cran.rstudio.com/src/contrib/mgcv_1.8-28.tar.gz'

Content type 'application/x-gzip' length 915991 bytes (894 KB)

==================================================

downloaded 894 KB

trying URL 'https://cran.rstudio.com/src/contrib/plyr_1.8.4.tar.gz'

Content type 'application/x-gzip' length 392451 bytes (383 KB)

==================================================

downloaded 383 KB

trying URL 'https://cran.rstudio.com/src/contrib/reshape2_1.4.3.tar.gz'

Content type 'application/x-gzip' length 36405 bytes (35 KB)

==================================================

downloaded 35 KB

trying URL 'https://cran.rstudio.com/src/contrib/scales_1.0.0.tar.gz'

Content type 'application/x-gzip' length 299262 bytes (292 KB)

==================================================

downloaded 292 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'stats' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/viridisLite_0.3.0.tar.gz'

Content type 'application/x-gzip' length 44019 bytes (42 KB)

==================================================

downloaded 42 KB

trying URL 'https://cran.rstudio.com/src/contrib/withr_2.1.2.tar.gz'

Content type 'application/x-gzip' length 53578 bytes (52 KB)

==================================================

downloaded 52 KB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'graphics' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/nlme_3.1-139.tar.gz'

Content type 'application/x-gzip' length 793473 bytes (774 KB)

==================================================

downloaded 774 KB

trying URL 'https://cran.rstudio.com/src/contrib/Matrix_1.2-17.tar.gz'

Content type 'application/x-gzip' length 1860456 bytes (1.8 MB)

==================================================

downloaded 1.8 MB

Warning in download.packages(packages, destdir = file.path(mainDir, subDir)) :

  no package 'splines' at the repositories

trying URL 'https://cran.rstudio.com/src/contrib/stringr_1.4.0.tar.gz'

Content type 'application/x-gzip' length 135777 bytes (132 KB)

==================================================

downloaded 132 KB

trying URL 'https://cran.rstudio.com/src/contrib/labeling_0.3.tar.gz'

Content type 'application/x-gzip' length 10722 bytes (10 KB)

==================================================

downloaded 10 KB

trying URL 'https://cran.rstudio.com/src/contrib/munsell_0.5.0.tar.gz'

Content type 'application/x-gzip' length 182653 bytes (178 KB)

==================================================

downloaded 178 KB

trying URL 'https://cran.rstudio.com/src/contrib/RColorBrewer_1.1-2.tar.gz'

Content type 'application/x-gzip' length 11532 bytes (11 KB)

==================================================

downloaded 11 KB

trying URL 'https://cran.rstudio.com/src/contrib/lattice_0.20-38.tar.gz'

Content type 'application/x-gzip' length 359031 bytes (350 KB)

==================================================

downloaded 350 KB

trying URL 'https://cran.rstudio.com/src/contrib/colorspace_1.4-1.tar.gz'

Content type 'application/x-gzip' length 2152594 bytes (2.1 MB)

==================================================

downloaded 2.1 MB

trying URL 'https://cran.rstudio.com/src/contrib/stringi_1.4.3.tar.gz'

Content type 'application/x-gzip' length 7290890 bytes (7.0 MB)

==================================================

downloaded 7.0 MB

      [,1]           [,2]                                                       

 [1,] "dplyr"        "/Users/ihongdon/Downloads/r-pkg/dplyr_0.8.0.1.tar.gz"     

 [2,] "ggplot2"      "/Users/ihongdon/Downloads/r-pkg/ggplot2_3.1.1.tar.gz"     

 [3,] "assertthat"   "/Users/ihongdon/Downloads/r-pkg/assertthat_0.2.1.tar.gz"  

 [4,] "glue"         "/Users/ihongdon/Downloads/r-pkg/glue_1.3.1.tar.gz"        

 [5,] "magrittr"     "/Users/ihongdon/Downloads/r-pkg/magrittr_1.5.tar.gz"      

 [6,] "pkgconfig"    "/Users/ihongdon/Downloads/r-pkg/pkgconfig_2.0.2.tar.gz"   

 [7,] "R6"           "/Users/ihongdon/Downloads/r-pkg/R6_2.4.0.tar.gz"          

 [8,] "Rcpp"         "/Users/ihongdon/Downloads/r-pkg/Rcpp_1.0.1.tar.gz"        

 [9,] "rlang"        "/Users/ihongdon/Downloads/r-pkg/rlang_0.3.4.tar.gz"       

[10,] "tibble"       "/Users/ihongdon/Downloads/r-pkg/tibble_2.1.1.tar.gz"      

[11,] "tidyselect"   "/Users/ihongdon/Downloads/r-pkg/tidyselect_0.2.5.tar.gz"  

[12,] "cli"          "/Users/ihongdon/Downloads/r-pkg/cli_1.1.0.tar.gz"         

[13,] "crayon"       "/Users/ihongdon/Downloads/r-pkg/crayon_1.3.4.tar.gz"      

[14,] "fansi"        "/Users/ihongdon/Downloads/r-pkg/fansi_0.4.0.tar.gz"       

[15,] "pillar"       "/Users/ihongdon/Downloads/r-pkg/pillar_1.3.1.tar.gz"      

[16,] "purrr"        "/Users/ihongdon/Downloads/r-pkg/purrr_0.3.2.tar.gz"       

[17,] "utf8"         "/Users/ihongdon/Downloads/r-pkg/utf8_1.1.4.tar.gz"        

[18,] "digest"       "/Users/ihongdon/Downloads/r-pkg/digest_0.6.18.tar.gz"     

[19,] "gtable"       "/Users/ihongdon/Downloads/r-pkg/gtable_0.3.0.tar.gz"      

[20,] "lazyeval"     "/Users/ihongdon/Downloads/r-pkg/lazyeval_0.2.2.tar.gz"    

[21,] "MASS"         "/Users/ihongdon/Downloads/r-pkg/MASS_7.3-51.4.tar.gz"     

[22,] "mgcv"         "/Users/ihongdon/Downloads/r-pkg/mgcv_1.8-28.tar.gz"       

[23,] "plyr"         "/Users/ihongdon/Downloads/r-pkg/plyr_1.8.4.tar.gz"        

[24,] "reshape2"     "/Users/ihongdon/Downloads/r-pkg/reshape2_1.4.3.tar.gz"    

[25,] "scales"       "/Users/ihongdon/Downloads/r-pkg/scales_1.0.0.tar.gz"      

[26,] "viridisLite"  "/Users/ihongdon/Downloads/r-pkg/viridisLite_0.3.0.tar.gz

[27,] "withr"        "/Users/ihongdon/Downloads/r-pkg/withr_2.1.2.tar.gz"       

[28,] "nlme"         "/Users/ihongdon/Downloads/r-pkg/nlme_3.1-139.tar.gz"      

[29,] "Matrix"       "/Users/ihongdon/Downloads/r-pkg/Matrix_1.2-17.tar.gz"     

[30,] "stringr"      "/Users/ihongdon/Downloads/r-pkg/stringr_1.4.0.tar.gz"     

[31,] "labeling"     "/Users/ihongdon/Downloads/r-pkg/labeling_0.3.tar.gz"      

[32,] "munsell"      "/Users/ihongdon/Downloads/r-pkg/munsell_0.5.0.tar.gz"     

[33,] "RColorBrewer" "/Users/ihongdon/Downloads/r-pkg/RColorBrewer_1.1-2.tar.gz"

[34,] "lattice"      "/Users/ihongdon/Downloads/r-pkg/lattice_0.20-38.tar.gz"   

[35,] "colorspace"   "/Users/ihongdon/Downloads/r-pkg/colorspace_1.4-1.tar.gz"  

[36,] "stringi"      "/Users/ihongdon/Downloads/r-pkg/stringi_1.4.3.tar.gz"

 

위에 다운로드된 패키지들의 리스트를 보면 알 수 있는 것처럼, 'dplyr'과 'ggplot2'의 두 개 패키지를 다운로드 하려고 했더니 이들이 의존성을 가지고 있는 [3] 'assertthat' 패키지부터 ~ [36] 'stringi' 패키지까지 34개의 패키지가 추가로 다운로드 되었습니다. 

 

외부의 인터넷 연결이 되는 환경에서 다운받아 놓은 이들 R packages 파일들을 저장매체에 저장하여 가져가서 폐쇄망으로 되어 있는 서버에 복사를 한 후에, 수작업으로 R package 설치하면 되겠습니다. (이때 의존성 있는 패키지들까지 알아서 다 설치가 되도록 여러번 설치 작업을 반복해주면 됩니다. 

 

아래 코드를 사용하시면 다운로드 받아놓은 모든 패키지의 리스트를 읽어와서 수동으로 R 패키지 설치를 할 수 있습니다. 

 

## set working directory
setwd("C:/Users/hdlee/Documents/R") # set with yours

## getting the list of R packages downloaded
src_pkgs <- list.files("C:/Users/hdlee/Documents/R") # use yours

## install R packages manually using binary files
install.packages(src_pkgs, repos = NULL, type = "source")

 

 

##==============================================##

폐쇄망 환경에서 Greenplum DB에 R 패키지 설치하는 방법은 

== > http://gpdbkr.blogspot.com/2019/12/greenplum-6-plr-r.html

==> https://rfriend.tistory.com/442 

포스팅을 참고하세요. 

##==============================================##

 

많은 도움이 되었기를 바랍니다. 

728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 행 기준으로 숫자형 변수들의 합을 구한 다음에, 그룹별로 행 기준 합이 최대인 전체 행을 선별하는 방법을 소개하겠습니다. 


말로 설명한 내용만으로는 얼른 이해가 안 올 수도 있겠는데요, 이번에 해볼 내용은 아래의 이미지를 참고하시면 이해가 쉬울 듯 합니다. 




예제로 사용할 하나의 그룹 변수(V1)와 나머지 9개의 숫자형 변수(V2~V10)로 구성된 간단한 DataFrame을 만들어보겠습니다. 



> ##------------------------------------------------------

> ## selecting distinct object using dplyr chain operator

> ##------------------------------------------------------

> rm(list=ls())

> set.seed(123) # for reproducibility

> V1 <- c(rep("apple", 5), rep("banana", 5), rep("tomato", 5)) # group

> V2 <- sample(x=1:10, size=15, replace=T)

> V3 <- sample(x=1:10, size=15, replace=T)

> V4 <- sample(x=1:10, size=15, replace=T)

> V5 <- sample(x=1:10, size=15, replace=T)

> V6 <- sample(x=1:10, size=15, replace=T)

> V7 <- sample(x=1:10, size=15, replace=T)

> V8 <- sample(x=1:10, size=15, replace=T)

> V9 <- sample(x=1:10, size=15, replace=T)

> V10 <- sample(x=1:10, size=15, replace=T)

> df <- data.frame(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10)

> df

       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10

1   apple  3  9 10  2  7  3  2  9   7

2   apple  8  3 10  3  1  4  7 10   4

3   apple  5  1  7  5  4  7  4  7   4

4   apple  9  4  8  3  3  4  7  5   3

5   apple 10 10  1  9  9  2  4  2   4

6  banana  1  9  5  1  5  3  2 10  10

7  banana  6  7  8  5  9  7  8  4   2

8  banana  9  7  3  8  9  5  1  1   1

9  banana  6 10  4  2  8  8  5 10   2

10 banana  5  7  3  6  5  2  6  8   7

11 tomato 10  8  2  3  8  5  6  2   7

12 tomato  5  6  5  2  7 10  4  6   9

13 tomato  7  6  5  8  8  9  5 10   7

14 tomato  6  3  4  9  1  9 10  6   8

15 tomato  2  2  2  4  5  2  5  5   6

 

> rm(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10)





rowSums() 함수를 사용하여서 행(row) 기준의 숫자형 변수들 모두에 대해 합계를 구하여 'V_sum' 이라는 새로운 변수를 추가해보겠습니다. 



> # summation in a row direction

> df$V_sum <- rowSums(df[,2:10])

> df

       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V_sum


1   apple  3  9 10  2  7  3  2  9   7    52

2   apple  8  3 10  3  1  4  7 10   4    50

3   apple  5  1  7  5  4  7  4  7   4    44

4   apple  9  4  8  3  3  4  7  5   3    46

5   apple 10 10  1  9  9  2  4  2   4    51


6  banana  1  9  5  1  5  3  2 10  10    46

7  banana  6  7  8  5  9  7  8  4   2    56

8  banana  9  7  3  8  9  5  1  1   1    44

9  banana  6 10  4  2  8  8  5 10   2    55

10 banana  5  7  3  6  5  2  6  8   7    49


11 tomato 10  8  2  3  8  5  6  2   7    51

12 tomato  5  6  5  2  7 10  4  6   9    54

13 tomato  7  6  5  8  8  9  5 10   7    65

14 tomato  6  3  4  9  1  9 10  6   8    56

15 tomato  2  2  2  4  5  2  5  5   6    33

 




위의 행 기준 합계로 보면 'apple' 그룹에서는 1번째 행의 합이 52로 가장 크며, 'banana' 그룹에서는 7번째 행의 합이 56으로서 가장 크고, 'tomato' 그룹에서는 13번째 행의 합이 65로서 가장 큽니다. 이를 1번째, 7번째, 13번째 전체 행을 선별해보겠습니다. (빨간색으로 표시한 부분)



> library(dplyr)

> df_group_distinct_max <- df %>% 

+   arrange(V1, desc(V_sum)) %>% 

+   group_by(V1) %>% slice(1:1)

> df_group_distinct_max <- data.frame(df_group_distinct_max[1:10])

> df_group_distinct_max

      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10

1  apple  3  9 10  2  7  3  2  9   7

2 banana  6  7  8  5  9  7  8  4   2

3 tomato  7  6  5  8  8  9  5 10   7

 


* dplyr 패키지 사용법은 

https://rfriend.tistory.com/234 , 

https://rfriend.tistory.com/236

참고하시기 바랍니다


많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. ^^



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 


(1) R Environment 상에 존재하는 여러개의 DataFrame 중에서 이름이 특정 조건을 만족하는 DataFrame을 선별하여 

 : ls(pattern = "xx")


(2) 하나의 List 로 묶는 방법, 

 : mget()


(3) List로 부터 특정 DataFrame을 Indexing 하는 방법

 : list[[1]], or  list[["name"]]


에 대해서 소개하겠습니다. 



먼저, 이번 포스팅의 예제의 특성 상 Environment 와 Console을 깨끗하게 청소하는 것부터 시작하겠습니다.  rm(list=ls()) 로 Environment에 있는 모든 객체를 삭제하고, cat("\014")로 Console을 백지상태로 만들어주었습니다. 



# To make an environment, console clear 

rm(list=ls()) # clearing environment

cat("\014") # clearing Console




객체 이름에 'data_'를 공통으로 포함한 3개의 DataFrame 예제와, 객체 이름에 'file_'을 포함한 3개의 DataFrame 예제를 만들어보겠습니다. 



# To make several sample DataFrames

data_1 <- data.frame(var1 = c(1, 2), var2 = c(3, 4))

data_2 <- data.frame(var1 = c('a', 'b'), var2 = c('c', 'd'))

data_3 <- data.frame(var1 = c(TRUE, TRUE), var2 = c(FALSE, TRUE))

file_1 <- data.frame(var1 = c(1, 2), var2 = c(3, 4))

file_2 <- data.frame(var1 = c('a', 'b'), var2 = c('c', 'd'))

file_3 <- data.frame(var1 = c(TRUE, TRUE), var2 = c(FALSE, TRUE))

  [DataFrame samples]

 



Environment에 생성된 객체를 확인해보려면 ls() 함수를 사용하면 됩니다. 그리고 특정 문자열을 포함한 객체만을 선별해서 찾아보려면 ls(pattern = "xx") 처럼 pattern = "xx" 를 추가하면 됩니다. 



> # To list up all objects in an environment

> ls()

[1] "data_1" "data_2" "data_3" "file_1" "file_2" "file_3"

> # To list up objects which have a matching 'pattern' in a DataFrame name

> ls(pattern = "data_")

[1] "data_1" "data_2" "data_3"

> ls(pattern = "file_")

[1] "file_1" "file_2" "file_3"

 




다음으로, mget() 함수를 사용하여 ls(pattern = "data_"), ls(pattern = "file_"로 각각 선별한 DataFrame 객체들을 2개의 List로 각각 묶어서 생성해보겠습니다.  



> # To combine all DataFrame into a List using mget()

> list_df_data <- mget(ls(pattern = "data_"))

> list_df_data

$data_1

  var1 var2

1    1    3

2    2    4


$data_2

  var1 var2

1    a    c

2    b    d


$data_3

  var1  var2

1 TRUE FALSE

2 TRUE  TRUE


> list_df_file <- mget(ls(pattern = "file_"))

> list_df_file

$file_1

  var1 var2

1    1    3

2    2    4


$file_2

  var1 var2

1    a    c

2    b    d


$file_3

  var1  var2

1 TRUE FALSE

2 TRUE  TRUE






마지막으로, List에 묶인 DataFrame을 [[ ]] 을 사용하여 위치(숫자) 혹은 이름으로 Indexing 하는 방법을 아래에 소개합니다. 



> # To index one of object in a List using [[ ]]

> list_df_data[[1]] # using 'number'

  var1 var2

1    1    3

2    2    4

> list_df_data["data_1"] # using 'name'

$data_1

  var1 var2

1    1    3

2    2    4 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾸욱 눌러주세요.



728x90
반응형
Posted by Rfriend
,

문자열이나 숫자를 특정 형식으로 길이를 지정해주면 데이터를 출력했을 때 깨끗하게 정리가 되어 보이기 때문에 가독성이 좋아집니다. 


혹은 데이터가 특정 형식(format)으로 DB에 이미 지정이 되어 있어서 데이터 간 병합이나 join을 하기 위해 특정 형식으로 데이터를 표준화 해주어야 할 경우가 있습니다. 


이번 포스팅에서는 {base} package의 sprintf() 함수를 사용해서 


 - (1) 문자열을 매개변수 width 길이로 만들고, 빈 자리는 '0'으로 채우기 : sprintf("%05d", var)


 - (2) 소수점 숫자(numeric)의 자리수를 지정해주기 : sprintf(".5f", var)


하는 방법에 대해서 알아보겠습니다. 


이번 포스팅의 함수 sprintf()는 데이터 전처리할 때 종종 사용하는 편이예요. 



 (1) 문자열을 특정 길이로 만들고, 빈 자리수만큼 '0'을 채우기 : sprintf("%05d", var)





1자리, 2자리, 3자리, 4자리를 가진 데이터를 가지고 예제로 사용할 간단한 DataFrame을 만들어보겠습니다. 



> # making a sample DataFrame

> df <- data.frame(var1 = c(1, 11, 111, 1111))

> df

  var1

1    1

2   11

3  111

4 1111

 




위의 예제 데이터셋을 가지고, 칼럼 var1의 데이터를 


- '1 자리수를 가진 문자열'로 만들되, '1자리수가 안되면 모자라는 자리수 만큼'0'으로 채우기'

- '2 자리수를 가진 문자열'로 만들되, '2자리수가 안되면 모자라는 자리수 만큼'0'으로 채우기'

- '3 자리수를 가진 문자열'로 만들되, '3자리수가 안되면 모자라는 자리수 만큼'0'으로 채우기'

- '4 자리수를 가진 문자열'로 만들되, '4자리수가 안되면 모자라는 자리수 만큼'0'으로 채우기'

- '5 자리수를 가진 문자열'로 만들되, '5자리수가 안되면 모자라는 자리수 만큼'0'으로 채우기'


를 해보겠습니다. 


만약 매개변수 자리수보다 데이터의 길이가 더 크다면 '0'이 채워지지는 않습니다.  아래의 예제의 결과를 View(df)로 해서 보면 원래의 변수 var1과 sprintf() 함수를 사용해서 만든 var1_01d 변수의 데이터 출력 형식이 다른 것을 알 수 있습니다. 


그리고 class 함수로 데이터 형식을 살펴보니 원래 변수 var1은 숫자형(numeric)이지만 sprintf() 함수로 만든 새로운 변수는 요인형(factor)의 문자열로 바뀌어 있음을 알 수 있습니다. 



> #-------------------------

> # (1) sprintf(%03d, var) : Format number as fixed width, with leading zeros

> df <- transform(df, 

+                 var1_01d = sprintf("%01d", var1), 

+                 var1_02d = sprintf("%02d", var1), 

+                 var1_03d = sprintf("%03d", var1), 

+                 var1_04d = sprintf("%04d", var1), 

+                 var1_05d = sprintf("%05d", var1))

> df

  var1 var1_01d var1_02d var1_03d var1_04d var1_05d

1    1        1       01      001     0001    00001

2   11       11       11      011     0011    00011

3  111      111      111      111     0111    00111

4 1111     1111     1111     1111     1111    01111



> View(df)



> sapply(df, class)

     var1  var1_01d  var1_02d  var1_03d  var1_04d  var1_05d

"numeric"  "factor"  "factor"  "factor"  "factor"  "factor"




 (2) 소수점 숫자(numeric)의 자리수를 지정해주기 : sprintf(".5f", var)


무리수인 자연상수 e의 소수점 10째 자리까지의 수를 대상으로 sprintf("%.5f", e) 함수를 사용해서 소수점의 자리수를 설정해보겠습니다. "%.숫자f"의 숫자 만큼 소수점을 표시해주는데요, 반올림을 해서 표시해줍니다. 아래의 예제를 보시면 금방 이해할 수 있을 것입니다. 



> #-------------------------

> # (3) sprintf("%.5f", x) : formatting decimal point, 

> e <- c(2.7182818284) # mathematical constant, the base of the natural logarithm

> sprintf("%.0f", e)

[1] "3"

> sprintf("%.1f", e)

[1] "2.7"

> sprintf("%.2f", e)

[1] "2.72"

> sprintf("%.3f", e)

[1] "2.718"

> sprintf("%.5f", e)

[1] "2.71828"

> sprintf("%.10f", e)

[1] "2.7182818284"

 




아래의 예시는 sprintf("%숫자.f, e)로 '숫자' 부분에 매개변수로 정수 부분의 자리수를 지정해주는 예시입니다. 소수점의 자리도 모두 포함해서 '숫자' 부분 매개변수만큼의 길이로 표시 형식을 맞추어줍니다. 



> e <- c(2.7182818284) # mathematical constant, the base of the natural logarithm

> sprintf("%1.1f", e)

[1] "2.7"

> sprintf("%2.1f", e)

[1] "2.7"

> sprintf("%3.1f", e)

[1] "2.7"

> sprintf("%5.1f", e)

[1] "  2.7"

> sprintf("%10.1f", e)

[1] "       2.7"

>  



많은 도움 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾸욱 눌러주세요. ^^





728x90
반응형
Posted by Rfriend
,

보통 외부 데이터 불러오기 할 때 {utils} package의 read.table() 함수를 사용하곤 합니다. 


크기가 작은 데이터라면 별 문제를 못느낄 텐데요, 만약 데이터 사이즈가 수 메가, 기가 단위의 큰 데이터라면 데이터를 불러들이는데 너무 오랜 시간이 걸려서 문제가 될 수 있습니다. 


대용량 데이터 처리에 아주 뛰어난 성능을 발휘하는 data.table 패키지의 fread() 함수를 사용하면 큰 용량의 외부 데이터도 빠르게 불러올 수 있습니다. 


(물론 R은 메모리에 데이터를 올려놓고 처리/분석을 하므로 하둡에서 말하는 수테라급의 대용량에는 필적을 못하구요, 분산병렬처리도 아니긴 합니다. 이 포스팅에서 말하는 대용량은 책보고 공부할 때 사용하는 수십, 수백개 row 를 가진 예제 데이터 대비 실전에서 사용하는 수십만, 수백만, 수천만 row 데이터를 말하는 것입니다. ^^;)


아래에 간단한 샘플 데이터를 만들어서 {utils} 패키지의 read.table() 함수와 {data.table} 패키지의 fread() 함수의 데이터 불러오는데 소요되는 시간을 비교해보았습니다. 



[ 외부 데이터 읽어오기 : {utils} 패키지 read.table() 함수 vs. {data.table} 패키지 fread(0 함수 ]




1. 샘플 데이터 만들기 : 1 백 만개 row, 변수 2개를 가지는 데이터 프레임



# generating large scaled data

my_data <- data.frame(var1 = rnorm(n = 1000000, mean = 0, sd = 1), 

                      var2 = rnorm(n = 1000000, mean = 2, sd = 3))

 



> str(my_data)

'data.frame': 1000000 obs. of  2 variables:

 $ var1: num  -0.556 1.787 0.498 -1.967 0.701 ...

 $ var2: num  1.669 0.597 4.452 1.405 6.936 ...

 



# exporting to text file

write.table(my_data, 

            "/Users/Desktop/R/my_data.txt",

            sep = "|",

            row.names = FALSE, 

            quote = FALSE)

 




2. {utils} 패키지의 read.table() 함수를 사용해서 my_data.txt 불러오기


system.time() 함수로 데이터를 불어오는데 소요된 시간을 재어보았더니 7.287초가 나왔습니다. 

(매번 할 때마다 소요 시간이 조금씩 차이가 날 수는 있습니다)



# reading text file : (1) read.table() of {utils} package

> system.time(my_data_readtable <- read.table("/Users/Desktop/R/my_data.txt",

+                                             sep = "|", 

+                                             header = TRUE, 

+                                             stringsAsFactors = FALSE))

   user  system elapsed 

  7.161   0.100   7.287

 




3. {data.table} 패키지의 fread() 함수를 사용해서 my_data.txt 불러오기


data.table 패키지는 기본 패키지가 아니므로 먼저 별도 설치(install.packages) 및 호출(library)이 필요합니다. 

(매번 할 때마다 소요 시간이 조금씩 차이가 날 수는 있습니다)



# reading text file : (2) fread() of {data.table} package

install.packages("data.table")

library(data.table)

 



system.time() 함수로 my_data를 불러오는데 걸린 시간을 재어봤더니 0.256초가 걸렸습니다. 



> system.time(my_data_fread <- fread("/Users/Desktop/R/my_data.txt", 

+                                    sep = "|", 

+                                    header = TRUE, 

+                                    stringsAsFactors = FALSE))

   user  system elapsed 

  0.242   0.014   0.256

 




1백만 행을 가진 데이터프레임을 읽어오는데 있어, 앞서 read.table() 함수가 7.287 초 걸렸던데 반해, fread() 함수는 0.256 초밖에 걸리지 않았습니다.  fread() 함수는 read.table() 함수를 사용했을 때 대비 약 96.5% 정도 속도가 더 적게 걸린 것입니다.  놀랍지요?!!! 



> 0.256/7.287

[1] 0.03513106

 



R 은 대용량 데이터에는 맥을 못춰라고 지레 평가절하하기 보다는 {data.table} 패키지의 fread() 함수로 대용량 데이터 불러오기 속도 문제를 공략해보시지요. 


많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'을 꾸욱 눌러주세요. ^^


728x90
반응형
Posted by Rfriend
,

개발 프로젝트에 가보면 서버와 클라이언트 간 데이터 전송할 때 XML 보다 기능은 적지만 보다 간단하고 파싱도 빠른 JSON(JavaScript Object Notation, (/ˈsən/ JAY-sən)) 데이터 포맷을 많이 사용합니다. 


JSON 은 JavaScript Object Notation 이라는 이름처럼 JavaScript 에서 유래하기는 했습니다만, 프로그래밍 언어에 독립적으로 기능하는 데이터 포맷입니다. 


JSON 이 무엇인가를 이해하는데 있어, 어떤 사람을 기술하는데 JSON 표기를 사용한 아래의 예제를 참고하시면 도움이 될 듯 합니다. 



[ 사람을 기술하는데 사용한 JSON 표기 예시 (JSON representation describing a person) ]


{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

 * source : https://en.wikipedia.org/wiki/JSON




개발하는 분이라면 위 예제의 JSON 데이터 포맷이 아주 익숙할 것입니다만, 개발 경험이 없는 분석가의 경우 매우 생소하게 느낄 수 있습니다.  R에서 사용하는 데이터 구조로 스칼라, 벡터, 행렬, 배열, 데이터프레임, 리스트 등이 있는데요, 특히 행렬, 데이터프레임의 경우 2차원의 행(row)과 열(column)로 데이터셋이 구성이 되어 있습니다. JSON과는 많이 다르기 때문에 처음 JSON을 본 분석가는 아마 '이거 뭐지?' 하고 당황할 것 같습니다 (제가 그랬어요. ^^;).


그나마 위의 SJON 데이터는 들여쓰기(indentation)이 이쁘게 되어 있어서 가독성이 좋은 것이구요, 개발자들이 건네주는 SJON 파일을 보면 아래처럼 들여쓰기 없이 옆으로 죽~ 이어져 있어서 가독성이 매우 떨어지다 보니 더 당황하게 되는거 같습니다. @@~


{"firstName":"John","lastName":"Smith","isAlive":true,"age":25,"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021-3100"},"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"},{"type":"mobile","number":"123 456-7890"}],"children":[],"spouse":null} 



그래서 보통은 JSON parser 의 도움을 받아서 들여쓰기(indentation)을 해서 데이터 구조, 위계체계를 살펴보곤 합니다. 

아래는 http://www.jsonparseronline.com/ 이라는 사이트에 들어가서 JSON 데이터(왼쪽)를 parsing (오른쪽) 해본 것입니다. 


[ JSON parser (http://www.jsonparseronline.com) ]





위의 데이터를 자세히 보시면 "address""phoneNumbers"는 다른 항목과는 달리 하위에 nested data 들을 가지고 있습니다. 일종의 데이터 위계체계가 있는 셈인데요, 아래 예시처럼 어떤 사람에 대한 데이터를 JSON 데이터 포맷으로 구조화하는 방식이 합리적이고 효율적으로 보입니다. 다만, R이나 Python (numpy, pandas 모듈), SAS, SPSS 등의 분석 툴을 사용하는 분석가라면 생소할 수 있다는점을 빼면 말이지요. 





서론이 길었습니다. 


이번 포스팅에는 R의 jsonlite 패키지를 사용해서 


(1) JSON 포맷의 데이터를 R DataFrame 으로 변환

     (converting from JSON to R DataFrame)


(2) R DataFrame 을 JSON 포맷의 데이터로 변환

     (converting from R DataFrame to JSON)


하는 방법에 대해서 알아보겠습니다. 


jsonlite 패키지를 사용하면 JSON 데이터 포맷을 구조를 R이 알아서 잘 이해를 해서 R 분석가가 익숙한 R DataFrame으로 자동으로 바꾸어 주며, 그 반대로 R DataFrame을 JSON 데이터 포맷으로도 바꾸어주니 매우 편리한 패키지입니다. 



먼저, jsonlite 패키지와 (웹에서 JSON 데이터를 호출할 때 사용하는) httr 패키지를 설치하고 불러와 보겠습니다. 



install.packages("jsonlite")

library(jsonlite)


install.packages("httr")

library(httr)




 (1) JSON 포맷의 데이터를 R DataFrame 으로 변환 (converting from JSON to R DataFrame)

      : fromJSON() 함수


아래는 jsonlite 패키지의 fromJSON() 함수를 사용해서 "https://api.github.com/users/hadley/repos" 의 github API 로부터 JSON 데이터를 요청(request)해서 R DataFrame으로 변환해 본 예제입니다. 



[ (R로 불러오기 전의) JSON 원본 데이터 포맷 (https://api.github.com/users/hadley/repos) ]





자세히 보면 "owner" 는 nested data 로 login, id 등의 데이터를 가지고 있습니다. 






30개의 관측치와 69개의 변수를 가지고 있는 DataFrame으로 잘 변환이 되었음을 알 수 있습니다. 


[ R jsonlite package를 사용해서 JSON 데이터를 R DataFrame 으로 변환한 모습 ]



> # (1) converting JSON to R DataFrame

> df_repos <- fromJSON("https://api.github.com/users/hadley/repos")

> str(df_repos)

'data.frame': 30 obs. of  69 variables:

 $ id               : int  40423928 40544418 14984909 12241750 5154874 9324319 20228011 82348 888200 3116998 ...

 $ name             : chr  "15-state-of-the-union" "15-student-papers" "500lines" "adv-r" ...

 $ full_name        : chr  "hadley/15-state-of-the-union" "hadley/15-student-papers" "hadley/500lines" "hadley/adv-r" ...

 $ owner            :'data.frame': 30 obs. of  17 variables:

  ..$ login              : chr  "hadley" "hadley" "hadley" "hadley" ...

  ..$ id                 : int  4196 4196 4196 4196 4196 4196 4196 4196 4196 4196 ...

  ..$ avatar_url         : chr  "https://avatars0.githubusercontent.com/u/4196?v=3" "https://avatars0.githubusercontent.com/u/4196?v=3" "https://avatars0.githubusercontent.com/u/4196?v=3" "https://avatars0.githubusercontent.com/u/4196?v=3" ...

  ..$ gravatar_id        : chr  "" "" "" "" ...

  ..$ url                : chr  "https://api.github.com/users/hadley" "https://api.github.com/users/hadley" "https://api.github.com/users/hadley" "https://api.github.com/users/hadley" ...

  ..$ html_url           : chr  "https://github.com/hadley" "https://github.com/hadley" "https://github.com/hadley" "https://github.com/hadley" ...

  ..$ followers_url      : chr  "https://api.github.com/users/hadley/followers" "https://api.github.com/users/hadley/followers" "https://api.github.com/users/hadley/followers" "https://api.github.com/users/hadley/followers" ...

  ..$ following_url      : chr  "https://api.github.com/users/hadley/following{/other_user}" "https://api.github.com/users/hadley/following{/other_user}" "https://api.github.com/users/hadley/following{/other_user}" "https://api.github.com/users/hadley/following{/other_user}" ... 

 ...이하 생략 ...


> names(df_repos)

 [1] "id"                "name"              "full_name"         "owner"             "private"          

 [6] "html_url"          "description"       "fork"              "url"               "forks_url"        

[11] "keys_url"          "collaborators_url" "teams_url"         "hooks_url"         "issue_events_url" 

[16] "events_url"        "assignees_url"     "branches_url"      "tags_url"          "blobs_url"        

[21] "git_tags_url"      "git_refs_url"      "trees_url"         "statuses_url"      "languages_url"    

[26] "stargazers_url"    "contributors_url"  "subscribers_url"   "subscription_url"  "commits_url"      

[31] "git_commits_url"   "comments_url"      "issue_comment_url" "contents_url"      "compare_url"      

[36] "merges_url"        "archive_url"       "downloads_url"     "issues_url"        "pulls_url"        

[41] "milestones_url"    "notifications_url" "labels_url"        "releases_url"      "deployments_url"  

[46] "created_at"        "updated_at"        "pushed_at"         "git_url"           "ssh_url"          

[51] "clone_url"         "svn_url"           "homepage"          "size"              "stargazers_count" 

[56] "watchers_count"    "language"          "has_issues"        "has_projects"      "has_downloads"    

[61] "has_wiki"          "has_pages"         "forks_count"       "mirror_url"        "open_issues_count"

[66] "forks"             "open_issues"       "watchers"          "default_branch"   





nested data 를 가진 "owner" 에 딸린 데이터 항목으로 login, id 등 총 17개 데이터 항목이 있군요. 



> # nested DataFrame in owner

> names(df_repos$owner)

 [1] "login"               "id"                  "avatar_url"          "gravatar_id"        

 [5] "url"                 "html_url"            "followers_url"       "following_url"      

 [9] "gists_url"           "starred_url"         "subscriptions_url"   "organizations_url"  

[13] "repos_url"           "events_url"          "received_events_url" "type"               

[17] "site_admin"

 




nested data 를 가진 "owner" 변수에 대해 하위 변수까지 위계 구조를 반영해서 데이터를 indexing 해오는 방법은 아래를 참고하세요. 4가지 방법 모두 동일한 결과를 반환합니다. 



> # different indexing, the same results

> df_repos[1:3,]$owner$login

[1] "hadley" "hadley" "hadley"

> df_repos[1:3,"owner"]$login

[1] "hadley" "hadley" "hadley"

> df_repos$owner[1:3,"login"]

[1] "hadley" "hadley" "hadley"

> df_repos$owner[1:3,]$login

[1] "hadley" "hadley" "hadley"

 




 (2) R DataFrame 을 JSON 포맷의 데이터로 변환 (converting from R DataFrame to JSON)

       : toJSON() 함수


위에서 JSON 포맷 데이터를 웹에서 호출해서 "df_repos"라는 이름의 R DataFrame으로 변환을 했었는데요, 이번에는 jsonlite패키지의 toJSON() 함수를 사용해서 거꾸로 R DataFrame 을 원래의 JSON 데이터 포맷으로 변환해보겠습니다. 



> # (2) converting R DataFrame to JSON

> json_repos <- toJSON(df_repos)

 




R DataFrame 데이터를 JSON 데이터 포맷으로 변환한 결과를 cat() 함수, prettify() 함수, minify() 함수를 사용해서 차례대로 살펴보겠습니다. 


cat() 함수, minify() 함수는 R의 head() 함수와 기능이 비슷합니다. 

 


> cat(json_repos)

[{"id":40423928,"name":"15-state-of-the-union","full_name":"hadley/15-state-of-the-union","owner":{"login":"hadley","id":4196,"avatar_url":"https://avatars0.githubusercontent.com/u/4196?v=3","gravatar_id":"","url":"https://api.github.com/users/hadley","html_url":"https://github.com/hadley","followers_url":"https://api.github.com/users/hadley/followers","following_url":"https://api.github.com/users/hadley/following{/other_user}","gists_url":"https://api.github.com/users/hadley/gists{/gist_id}","starred_url":"https://api.github.com/users/hadley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadley/subscriptions","organizations_url":"https://api.github.com/users/hadley/orgs","repos_url":"https://api.github.com/users/hadley/repos","events_url":"https://api.github.com/users/hadley/events{/privacy}","received_events_url":"https://api.github.com/users/hadley/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hadley/15-s... <truncated>

 




# not including indentation, whitespace

minify(json_repos)

 






prettify() 함수는 들여쓰기, 공백을 사용해서 JSON 데이터를 파싱해서 표기해줌으로써 아래에 화면캡쳐해 놓은 것처럼 가독성이 매우 좋습니다.  nested data 를 가진 "owner" 데이터에 대해서 아래처럼 정확하게 원래의 JSON 데이터포맷으로 변환을 해놨습니다. jsonlite 패키지 참 똑똑하지요? ^^



# including indentation, whitespace

prettify(json_repos, indent = 4)




JSON 데이터 포맷 관련해서 serializeJSON, stream_in, stream_out 등 추가적인 기능이 필요한 분은 아래 [Reference]의 jsonlite package 매뉴얼을 참고하시기 바랍니다. 


[Reference] https://cran.r-project.org/web/packages/jsonlite/jsonlite.pdf


많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. ^^



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 외부의 엑셀(Excel) 파일로 존재하는 데이터를 RStudio 를 사용해서 R로 불러오기 하는 방법을 소개하겠습니다. 

 

저는 주로 DB에 직접 connect해서 데이터를 내리거나, 아니면 csv 나 txt 형태의 데이터를 R로 불러와서 분석에 사용하곤 합니다만(read.table() 함수 참고), 엑셀이 워낙 숫자 다루는데 강력하고 편리하다 보니 엑셀 데이터를 가져다가 R에서 사용해야 하는 경우도 있을 것입니다. 그리고 RStudio 에서 GUI로 Excel, SAS, SPSS, Stata 포맷을 데이터를 불러올 수 있는 기능이 있으니 클릭 몇 번으로 해결할 수 있으니 편리합니다. 

 

예제로 사용할 'cust_profile.xlsx'라는 이름의 엑셀 자료는 아래처럼 생겼습니다. 

(주의사항 1) 여러개의 sheet 중에서 'cust_profile' 라는 이름의 첫번재 sheet에 있는 데이터를 불러오고 싶습니다. 

(주의사항 2) 1~2번째 행(row)는 사용하지 않을 것이구요, 3번째 행부터 데이터를 불러오고 싶습니다.  1번째 열은 사용하지 않고 2번째 열부터 데이터를 불러오고 싶습니다. (정확히는 B3:E8 range)

(주의사항 3) 'gender' 열의 세번째 관측치 값이 결측값(missing value) 인데요, R로 불러왔을 때 결측값( 'NA')으로 잘 인식해서 불러오게 하고 싶습니다.  

 

[ 엑셀 예제 파일 ] 

* 실습 예제 엑셀 파일 첨부 ☞  

cust_profile.xlsx
다운로드

 

 

우선 R에서 "readxl" package를 설치하고 로딩해보겠습니다. 

 

(1) package 설치 및 로딩하기 

 

# installing and loading readxl package 

install.packages('readxl')

library(readxl)

 

 

 

엑셀 데이터를 R로 불러오는 방법에는 (a) R script를 이용하는 방법과,  (b) RStudio GUI 를 이용하는 방법의 2가지가 있습니다.  차례대로 소개하겠습니다. 

 

  (2) R script를 사용해서 엑셀 데이터 불러오기

 

엑셀 파일이 들어있는 경로(path), 사용할 sheet 이름, 불러올 데이터의 범위(range), 칼럼 이름(column name), 칼럼 유형(column type), cell 에 값이 비어있는 결측값에 사용할 문자열(비어 있으면 디폴트로 결측값으로 인식함)을 입력해주면 됩니다. 

 

> # importing excel file by using read_excel() function

> cust_profile <- read_excel("C:/Users/Administrator/Documents/cust_profile.xlsx", # path

+                            sheet = "cust_profile", # sheet name to read from

+                            range = "B3:E8", # cell range to read from

+                            col_names = TRUE, # TRUE to use the first row as column names

+                            col_types = "guess", # guess the types of columns

+                            na = "NA") # Character vector of strings to use for missing values

> str(cust_profile)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of  4 variables:

 $ ID    : chr  "c1" "c2" "c3" "c4" ...

 $ gender: chr  "F" "M" NA "M" ...

 $ age   : num  30 28 46 65 38

 $ name  : chr  "Kim" "Lee" "Park" "Moon" ...

 

> cust_profile

# A tibble: 5 × 4

     ID gender   age  name

  <chr>  <chr> <dbl> <chr>

1    c1      F    30   Kim

2    c2      M    28   Lee

3    c3   <NA>    46  Park

4    c4      M    65  Moon

5    c5      F    38  Choi

 

 

 

 

 

엑셀 데이터 크기가 커지면 행과 열의 범위(range)를 찾아서 정확히 입력하기가 번거로운 경우도 있을 텐데요, 아래처럼 처음의 2개의 행(row)은 무시하고(skip), 3번재 행부터 엑셀에서 데이터를 불러오라고 설정을 해주어도 결과는 똑같게 데이터를 불러올 수 있습니다. 

 

> # importing excel file using 'skip' option

> cust_profile_2 <- read_excel("C:/Users/Administrator/Documents/cust_profile.xlsx", # path

+                            sheet = "cust_profile", # sheet name to read from

+                            skip = 2, # Minimum number of rows to skip before reading anything

+                            col_names = TRUE, # TRUE to use the first row as column names

+                            col_types = "guess", # guess the types of columns

+                            na = "NA") # Character vector of strings to use for missing values

> cust_profile_2

# A tibble: 5 × 4

     ID gender   age  name

  <chr>  <chr> <dbl> <chr>

1    c1      F    30   Kim

2    c2      M    28   Lee

3    c3   <NA>    46  Park

4    c4      M    65  Moon

5    c5      F    38  Choi

 

 

 

다음으로 RStudio GUI 를 사용해서 클릭 몇 번으로 간단하게 엑셀 데이터 불러오는 방법을 소개하겠습니다. 

 

  (3) RStudio GUI 를 사용해서 엑셀 데이터 불러오기

 

(3-1) RStudio 우측 상단의 'Environments' 창에서 'Import Dataset' 메뉴를 선택한 후에, 'From Excel...' 하위 메뉴를 선택해보세요. 

 

 

(3-2) RStudio 의 'Import Excel Data' 창에서 왼쪽 하단의 'Import Options:' 화면에서 옵션을 입력하여 엑셀 데이터 불러오기

 

 

'Import Options:' 화면이 잘 안보일 것 같아서 아래에 확대해서 R Script 와 비교할 수 있도록 번호를 같이 표기해보았습니다. 

 

많은 도움 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~♡'를 꾸욱 눌러주세요. ^^

 

 

728x90
반응형
Posted by Rfriend
,