


R의 'reshape' 기능을 사용하여 데이터를 넓은 형식에서 긴 형식으로 변환하는 방법
R에서 'reshape' 함수는 데이터를 넓은 형식(열이 많음)에서 긴 형식(열이 적음)으로 변환하는 데 사용됩니다. 와이드 형식은 "행렬" 또는 "데이터 프레임"이라고도 하며 긴 형식은 "데이터 프레임" 또는 "테이블"이라고도 합니다. 'reshape' 함수는 두 가지 인수를 사용합니다. 첫 번째는 데이터입니다. 두 번째는 데이터를 어떻게 재구성해야 하는지에 대한 사양입니다. 두 번째 인수는 문자열이거나 수식일 수 있습니다. 문자열인 경우 재구성할 데이터가 포함된 변수의 이름을 지정합니다. 공식인 경우 변환을 정의하는 공식을 지정합니다.
다음은 R에서 `reshape`를 사용하는 몇 가지 예입니다:
```
# 샘플 데이터 만들기
data <- data.frame(id = c(1, 2) , 3, 4, 5),
변수 = c("x", "y", "z"),
값 = c(10, 20, 30, 40, 50))
# 데이터를 넓은 범위에서 다음으로 변경합니다. long format
reshape_data <- reshape(data, idvar = "id", timevar = "variables", direct = "long")
# 재구성된 데이터 보기
print(reshape_data)
```
이 예에서는 두 개의 샘플 데이터를 생성합니다. 변수(`x`, `y`, `z`)와 5개의 관측값(`id` = 1~5). 그런 다음 'reshape'를 사용하여 'id'가 id 변수이고 'variables'가 시간 변수임을 지정하여 데이터를 와이드 형식에서 긴 형식으로 변환합니다. 결과로 재구성된 데이터는 'reshape_data'에 저장됩니다. 마지막으로 `print`를 사용하여 재구성된 데이터를 인쇄합니다. `direction = "long"` 대신 `direction = "wide"`를 지정하여 `reshape`를 사용하여 데이터를 긴 형식에서 넓은 형식으로 변환할 수도 있습니다.



