Стоит рассмотреть хорошие имена столбцов (например, "<variable_chr>.<time_num>"
).Но мы можем исправить это за секунду.
pfx <- c("first", "second", "third", "fourth")
names(sample_dat)[-1] <- sapply(names(sample_dat)[-1], function(x) {
x <- gsub("_pick", "", x)
if (lengths(strsplit(x, "_")) == 2)
sub("(^.*)_(.*)", paste("\\2", which(pfx == sub("(^.*)_.+", "\\1", x)), sep="."), x)
else
paste0("rank.", which(pfx == x))
})
names(sample_dat) # good names now
# [1] "subject_id" "rank.1" "image.1" "neuro.1" "sex.1" "rank.2"
# [7] "image.2" "neuro.2" "sex.2" "rank.3" "image.3" "neuro.3"
# [13] "sex.3" "rank.4" "image.4" "neuro.4" "sex.4"
После этого мы можем легко использовать reshape
.
reshape(sample_dat, idvar="subject_id", varying=2:17, direction="long",
new.row.names=seq(ncol(sample_dat) - 1))
# subject_id time rank image neuro sex
# 1 sj1 1 1 a TD F
# 2 sj1 1 1 b TD F
# 3 sj1 1 1 c TD F
# 4 sj1 1 1 d TD F
# 5 sj1 2 2 e TD M
# 6 sj1 2 2 f TD M
# 7 sj1 2 2 g TD M
# 8 sj1 2 2 h TD M
# 9 sj1 3 3 i DS F
# 10 sj1 3 3 j DS F
# 11 sj1 3 3 k DS F
# 12 sj1 3 3 l DS F
# 13 sj1 4 4 m DS M
# 14 sj1 4 4 n DS M
# 15 sj1 4 4 o DS M
# 16 sj1 4 4 p DS M
Данные
sample_dat <- structure(list(subject_id = structure(c(1L, 1L, 1L, 1L), .Label = "sj1", class = "factor"),
first_pick = c(1, 1, 1, 1), first_image_pick = structure(1:4, .Label = c("a",
"b", "c", "d"), class = "factor"), first_pick_neuro = structure(c(1L,
1L, 1L, 1L), .Label = "TD", class = "factor"), first_pick_sex = structure(c(1L,
1L, 1L, 1L), .Label = "F", class = "factor"), second_pick = c(2,
2, 2, 2), second_image_pick = structure(1:4, .Label = c("e",
"f", "g", "h"), class = "factor"), second_pick_neuro = structure(c(1L,
1L, 1L, 1L), .Label = "TD", class = "factor"), second_pick_sex = structure(c(1L,
1L, 1L, 1L), .Label = "M", class = "factor"), third_pick = c(3,
3, 3, 3), third_image_pick = structure(1:4, .Label = c("i",
"j", "k", "l"), class = "factor"), third_pick_neuro = structure(c(1L,
1L, 1L, 1L), .Label = "DS", class = "factor"), third_pick_sex = structure(c(1L,
1L, 1L, 1L), .Label = "F", class = "factor"), fourth_pick = c(4,
4, 4, 4), fourth_image_pick = structure(1:4, .Label = c("m",
"n", "o", "p"), class = "factor"), fourth_pick_neuro = structure(c(1L,
1L, 1L, 1L), .Label = "DS", class = "factor"), fourth_pick_sex = structure(c(1L,
1L, 1L, 1L), .Label = "M", class = "factor")), class = "data.frame", row.names = c(NA,
-4L))