ベース R を使用reshape():
temp = read.delim(text="a,,,b,,
x,y,z,x,y,z
10,1,5,22,1,6
12,2,6,21,3,5
12,2,7,11,3,7
13,1,4,33,2,8
12,2,5,44,1,9", header=TRUE, skip=1, sep=",")
names(temp)[1:3] = paste0(names(temp[1:3]), ".0")
OUT = reshape(temp, direction="long", ids=rownames(temp), varying=1:ncol(temp))
OUT
# time x y z id
# 1.0 0 10 1 5 1
# 2.0 0 12 2 6 2
# 3.0 0 12 2 7 3
# 4.0 0 13 1 4 4
# 5.0 0 12 2 5 5
# 1.1 1 22 1 6 1
# 2.1 1 21 3 5 2
# 3.1 1 11 3 7 3
# 4.1 1 33 2 8 4
# 5.1 1 44 1 9 5
基本的に、3 列ごとに文字 ag がある最初の行をスキップする必要があります。サブ列名はすべて同じであるため、R は 3 番目の列の後のすべての列の後にグループ化番号を自動的に追加します。そのため、最初の 3 つの列にグループ化番号を追加する必要があります。
次に、「id」変数を作成するか、ここで行ったように、ID に行名を使用することができます。
次のように、「時間」変数を「セル」変数に変更できます。
# Change the following to the number of levels you actually have
OUT$cell = factor(OUT$time, labels=letters[1:2])
次に、「時間」列をドロップします。
OUT$time = NULL
アップデート
以下のコメントの質問に答えるには、最初のラベルが文字以外の場合でも問題ありません。私が取るシーケンスは次のとおりです。
temp = read.csv("path/to/file.csv", skip=1, stringsAsFactors = FALSE)
GROUPS = read.csv("path/to/file.csv", header=FALSE,
nrows=1, stringsAsFactors = FALSE)
GROUPS = GROUPS[!is.na(GROUPS)]
names(temp)[1:3] = paste0(names(temp[1:3]), ".0")
OUT = reshape(temp, direction="long", ids=rownames(temp), varying=1:ncol(temp))
OUT$cell = factor(temp$time, labels=GROUPS)
OUT$time = NULL