2017年4月28日 星期五

melt() 使用時 measure variable 的型態要一致

使用 melt() 來 reshape 資料時, measure variable 的型態要一致,否則會出現警告。

例如有以下的資料:
# 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)
view raw melt_trap.r hosted with ❤ by GitHub


資料中,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.

沒有留言: