IT박스

Gather (tidyr)와 용융 (reshape2) 비교

itboxs 2020. 11. 25. 07:47
반응형

Gather (tidyr)와 용융 (reshape2) 비교


저는 reshape2 패키지가 마음에 듭니다 . 삶을 너무 쉽게 만들었 기 때문입니다. 일반적으로 Hadley는 효율적이고 빠른 실행 코드를 가능하게하는 이전 패키지를 개선했습니다. 나는 생각 나는 좋겠주고 tidyr 소용돌이와 내가 생각 읽는 것과 gather매우 유사 melt에서 reshape2 . 그러나 문서를 읽은 후에 gather는 동일한 작업을 수행 할 수 없습니다 melt.

데이터보기

다음은 데이터보기입니다 ( dput게시물 끝에 형식의 실제 데이터 ).

  teacher yr1.baseline     pd yr1.lesson1 yr1.lesson2 yr2.lesson1 yr2.lesson2 yr2.lesson3
1       3      1/13/09 2/5/09      3/6/09     4/27/09     10/7/09    11/18/09      3/4/10
2       7      1/15/09 2/5/09      3/3/09      5/5/09    10/16/09    11/18/09      3/4/10
3       8      1/27/09 2/5/09      3/3/09     4/27/09     10/7/09    11/18/09      3/5/10

암호

다음은 melt패션 의 코드입니다 gather. 어떻게 gather똑같은 일을 할 수 melt있습니까?

library(reshape2); library(dplyr); library(tidyr)

dat %>% 
   melt(id=c("teacher", "pd"), value.name="date") 

dat %>% 
   gather(key=c(teacher, pd), value=date, -c(teacher, pd)) 

원하는 출력

   teacher     pd     variable     date
1        3 2/5/09 yr1.baseline  1/13/09
2        7 2/5/09 yr1.baseline  1/15/09
3        8 2/5/09 yr1.baseline  1/27/09
4        3 2/5/09  yr1.lesson1   3/6/09
5        7 2/5/09  yr1.lesson1   3/3/09
6        8 2/5/09  yr1.lesson1   3/3/09
7        3 2/5/09  yr1.lesson2  4/27/09
8        7 2/5/09  yr1.lesson2   5/5/09
9        8 2/5/09  yr1.lesson2  4/27/09
10       3 2/5/09  yr2.lesson1  10/7/09
11       7 2/5/09  yr2.lesson1 10/16/09
12       8 2/5/09  yr2.lesson1  10/7/09
13       3 2/5/09  yr2.lesson2 11/18/09
14       7 2/5/09  yr2.lesson2 11/18/09
15       8 2/5/09  yr2.lesson2 11/18/09
16       3 2/5/09  yr2.lesson3   3/4/10
17       7 2/5/09  yr2.lesson3   3/4/10
18       8 2/5/09  yr2.lesson3   3/5/10

데이터

dat <- structure(list(teacher = structure(1:3, .Label = c("3", "7", 
    "8"), class = "factor"), yr1.baseline = structure(1:3, .Label = c("1/13/09", 
    "1/15/09", "1/27/09"), class = "factor"), pd = structure(c(1L, 
    1L, 1L), .Label = "2/5/09", class = "factor"), yr1.lesson1 = structure(c(2L, 
    1L, 1L), .Label = c("3/3/09", "3/6/09"), class = "factor"), yr1.lesson2 = structure(c(1L, 
    2L, 1L), .Label = c("4/27/09", "5/5/09"), class = "factor"), 
        yr2.lesson1 = structure(c(2L, 1L, 2L), .Label = c("10/16/09", 
        "10/7/09"), class = "factor"), yr2.lesson2 = structure(c(1L, 
        1L, 1L), .Label = "11/18/09", class = "factor"), yr2.lesson3 = structure(c(1L, 
        1L, 2L), .Label = c("3/4/10", "3/5/10"), class = "factor")), .Names = c("teacher", 
    "yr1.baseline", "pd", "yr1.lesson1", "yr1.lesson2", "yr2.lesson1", 
    "yr2.lesson2", "yr2.lesson3"), row.names = c(NA, -3L), class = "data.frame")

귀하의 gather줄은 다음과 같은 모양입니다 :

dat %>% gather(variable, date, -teacher, -pd)

This says "Gather all variables except teacher and pd, calling the new key column 'variable' and the new value column 'date'."


As an explanation, note the following from the help(gather) page:

 ...: Specification of columns to gather. Use bare variable names.
      Select all variables between x and z with ‘x:z’, exclude y
      with ‘-y’. For more options, see the select documentation.

Since this is an ellipsis, the specification of columns to gather is given as separate (bare name) arguments. We wish to gather all columns except teacher and pd, so we use -.


In tidyr 1.0.0 this task is accomplished with the more flexible pivot_longer().

The equivalent syntax would be

library(tidyr)
dat %>% pivot_longer(cols = -c(teacher, pd), names_to = "variable", values_to = "date")

which says, correspondingly, "pivot everything longer except teacher and pd, calling the new variable column "variable" and the new value column "date".

Note that the long data comes back in order firstly of the columns of the previous data frame that were pivoted, unlike from gather, which came back in the order of the new variable column. To rearrange the resultant tibble, use dplyr::arrange().

참고URL : https://stackoverflow.com/questions/26536251/comparing-gather-tidyr-to-melt-reshape2

반응형