我认为这得到了你需要的格式:
dd <- data.frame(Section = 1:5, ID0 = 10001:10005, Totaltime = 1:5 * 100,
'Item1/Word' = '1/word', 'Item1/Cat' = '1/cat',
'Item1/Time' = '1/time',
'Item2/Word' = '2/word', 'Item2/Cat' = '2/cat',
'Item2/Time' = '2/time',
'Item3/Word' = '3/word', 'Item3/Cat' = '3/cat',
'Item3/Time' = '3/time', stringsAsFactors = FALSE,
check.names = FALSE)
# Section ID0 Totaltime Item1/Word Item1/Cat Item1/Time Item2/Word Item2/Cat Item2/Time Item3/Word Item3/Cat Item3/Time
# 1 1 10001 100 1/word 1/cat 1/time 2/word 2/cat 2/time 3/word 3/cat 3/time
# 2 2 10002 200 1/word 1/cat 1/time 2/word 2/cat 2/time 3/word 3/cat 3/time
# 3 3 10003 300 1/word 1/cat 1/time 2/word 2/cat 2/time 3/word 3/cat 3/time
# 4 4 10004 400 1/word 1/cat 1/time 2/word 2/cat 2/time 3/word 3/cat 3/time
# 5 5 10005 500 1/word 1/cat 1/time 2/word 2/cat 2/time 3/word 3/cat 3/time
## define the varying columns:
keys <- paste0('Item', 1:3)
keys <- c('Word','Cat','Time')
l <- lapply(keys, function(x) grep(x, names(dd)))
rr <- reshape(dd, direction = 'long', varying = l)
rr <- rr[with(rr, order(Section, ID0, Totaltime)),
## `reshape` makes two extra variabes, time and id, we dont want
-which(names(rr) %in% c('id','time'))]
rr[, 1:3] <- lapply(rr[, 1:3], function(x) ifelse(duplicated(x), '', x))
`rownames<-`(rr, NULL)
# Section ID0 Totaltime Item1/Word Item1/Cat Item1/Time
# 1 1 10001 100 1/word 1/cat 1/time
# 2 2/word 2/cat 2/time
# 3 3/word 3/cat 3/time
# 4 2 10002 200 1/word 1/cat 1/time
# 5 2/word 2/cat 2/time
# 6 3/word 3/cat 3/time
# 7 3 10003 300 1/word 1/cat 1/time
# 8 2/word 2/cat 2/time
# 9 3/word 3/cat 3/time
# 10 4 10004 400 1/word 1/cat 1/time
# 11 2/word 2/cat 2/time
# 12 3/word 3/cat 3/time
# 13 5 10005 500 1/word 1/cat 1/time
# 14 2/word 2/cat 2/time
# 15 3/word 3/cat 3/time