目前我正在使用 R 来转换我的表格数据:
ID Code Condition WT
104 KEENTRAN CON4 .30577
. . . .
. . . .
该链接应该适用于任何想要下载我的数据框的人,否则这里是一个子集:
>dput(head(df))
structure(list(ID = c(104L, 368L, 10632L, 20385L, 24361L, 34378L
), Code = c("KEENTRAN", "ALEXEXPR", "MINNEXPMN", "JACKMOVWI",
"FREICOIN", "JBXEXPGA"), Condition = c("CON4", "CON4", "CON2",
"CON2", "CON6", "CON5"), WT = c(0.3057717456, 0.7909870604, 1,
1, 0.4301040524, 0.5977268575)), .Names = c("ID", "Code", "Condition",
"WT"), class = c("tbl_df", "data.frame"), row.names = c(NA, -6L
))
背景
我的示例数据是长格式,其中 Condition 变量的范围从“CON1”到“CON6”,我想将我的数据重新转换为宽格式,其中ID和Code值将是主键,而Condition的级别将是其列值取该特定 ID、代码、条件分组的WT最大值(如果不存在此类配对,则为零)。dcast()
这可以使用包中的函数在 R 中轻松完成reshape2
:
library(reshape2)
Result <- df %>% group_by(ID, Condition) %>%
summarise(value = max(as.numeric(WT))) %>%
dcast(ID ~ Condition)
Result[is.na(Result)] <- 0
我想在 SQL Server 中复制这个数据操作过程,但我不确定如何最好地做到这一点。非常感谢任何帮助或见解。