Online training class for Clinical R programming batch starts on Monday, 02Feb2026.
Click here for details.
library(tidyverse)
long <- tribble(
~usubjid, ~lbtestcd, ~lbstresn,
"1001", "HGB", 13,
"1001", "ALT", 30,
"1001", "AST", 23,
"1002", "HGB", 12,
"1002", "ALT", 28,
"1002", "AST", 15
)
wide <- long %>%
pivot_wider(
id_cols = usubjid,
names_from = lbtestcd,
values_from = lbstresn
) long <- data.frame(
usubjid = c(1001, 1001, 1001, 1002, 1002, 1002),
lbtestcd = c("HGB", "ALT", "AST", "HGB", "ALT", "AST"),
lbstresn = c(13, 30, 23, 12, 28, 15)
, stringsAsFactors = FALSE
)
wide <- reshape(
long,
idvar = "usubjid",
timevar = "lbtestcd",
direction = "wide",
v.names = "lbstresn"
)
names(wide) <- sub("^lbstresn\\.", "", names(wide))