R분석

reshape2 package

AIHYEONJI 2025. 2. 7. 16:41
install.packages("reshape2")
library(reshape2)

dcast : 결과로 데이터프레임을 반환하는 cast함수이다.

acast : 결과로 배열/매트릭스를 반환하는 cast함수다.

# 1. dcast 예제
# 데이터는 reshape package의 마지막 데이터를 이어서 사용하였다.
score_wides <- dcast(melted, Student~Subject, value.var="Score")

# 결과값 타입확인
print(is.date.frame(score_wides))

print(score_wides)

# 결과
[1] TRUE
  Student Math Korean English
1    길동   90     95      85
2    보고   85     80      88
3    순신   80     85      90
# acast 예제
score_matrix <- acast(melted, Student~Subject, value.var="Score")
print(is.matrix(score_matrix))
print(score_matrix)

# 결과
[1] TRUE
     Math Korean English
길동   90     95      85
보고   85     80      88
순신   80     85      90