例如有以下的資料:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parts of Codes are from https://www.r-bloggers.com/reshape-and-aggregate-data-with-the-r-package-reshape2/ | |
require(reshape2) | |
x <- data.frame(subject = c("John", "Mary"), | |
SN = c(1,2), | |
time = c(1,1), | |
age = c(33,NA), | |
weight = c(90, NA), | |
height = c(2,2)) | |
melt(x,id.vars = "SN") | |
str(x) |
資料中,subject 欄位為 factor。若使用 subject 為 id.var 進行 melt,則會出現以下警告訊息:
因為,第一個欄位為 Factor 型態,melt() 把後面其他的欄位也視為 Factor,造成轉換時的錯誤。
Reference:
[1] thiagogm, 2013. Reshape and aggregate data with the R package reshape2, www.r-bloggers.com.