2

次のコードで作成された次の (簡略化された) データセットを変換する必要があります。

structure(list(W1.1 = structure(c(1L, NA, NA), .Names = c("case1", 
"case2", "case3"), .Label = "1", class = "factor"), R1.1 = structure(c(1L, 
NA, NA), .Names = c("case1", "case2", "case3"), .Label = "2", class = "factor"), 
    W1.2 = structure(c(NA, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R1.2 = structure(c(NA, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W2.1 = structure(c(NA, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R2.1 = structure(c(NA, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W2.2 = structure(c(1L, NA, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "2", class = "factor"), R2.2 = structure(c(1L, 
    NA, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W3.1 = structure(c(1L, NA, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R3.1 = structure(c(1L, 
    NA, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W3.2 = structure(c(1L, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R3.2 = structure(c(1L, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    age = structure(c(3L, 1L, 2L), .Names = c("case1", "case2", 
    "case3"), .Label = c("20", "48", "56"), class = "factor"), 
    gender = structure(c(2L, 1L, 2L), .Names = c("case1", "case2", 
    "case3"), .Label = c("female", "male"), class = "factor")), .Names = c("W1.1", 
"R1.1", "W1.2", "R1.2", "W2.1", "R2.1", "W2.2", "R2.2", "W3.1", 
"R3.1", "W3.2", "R3.2", "age", "gender"), row.names = c(NA, 3L
), class = "data.frame")

私が望む新しいデータの場合: - Rx.x 値、年齢、性別に関する情報を含む、すべての xx 専用の行。- Wx.x が 1 の場合にのみ行を返します。2 または NA の場合は必要ありません。

私の例では、このデータセットは次のようになります。

  incident type Where Reported age gender
1        1  1.1     1        2  56   male
2        2  3.1     1        1  56   male
3        3  3.2     1        1  56   male
4        4  1.2     1        1  20 female
5        5  2.1     1        1  20 female
6        6  3.2     1        1  20 female

注: "Where" 列は、1 の定数ベクトルである必要があるため省略できます。分析には必要ありません。

4

1 に答える 1

5

これは (ほとんどの場合) が取り組むべき問題reshape()です。元のデータセットが「temp」と呼ばれると仮定します。

まず、ワイドフォーマットからロングフォーマットに変形します。

temp.long <- reshape(temp, direction = "long",
                     idvar=c("age", "gender"), 
                     varying = which(!names(temp) %in% c("age", "gender")), 
                     sep = "")
temp.long
#               age gender time    W    R
# 56.male.1.1    56   male  1.1    1    2
# 20.female.1.1  20 female  1.1 <NA> <NA>
# 48.male.1.1    48   male  1.1 <NA> <NA>
# 56.male.1.2    56   male  1.2 <NA> <NA>
# 20.female.1.2  20 female  1.2    1    1
# 48.male.1.2    48   male  1.2 <NA> <NA>
# 56.male.2.1    56   male  2.1 <NA> <NA>
# 20.female.2.1  20 female  2.1    1    1
# 48.male.2.1    48   male  2.1 <NA> <NA>
# 56.male.2.2    56   male  2.2    2    1
# 20.female.2.2  20 female  2.2 <NA> <NA>
# 48.male.2.2    48   male  2.2 <NA> <NA>
# 56.male.3.1    56   male  3.1    1    1
# 20.female.3.1  20 female  3.1 <NA> <NA>
# 48.male.3.1    48   male  3.1 <NA> <NA>
# 56.male.3.2    56   male  3.2    1    1
# 20.female.3.2  20 female  3.2    1    1
# 48.male.3.2    48   male  3.2 <NA> <NA>

次に、クリーンアップを行います。

temp.long <- na.omit(temp.long)
temp.long <- temp.long[-which(temp.long$W == 2), ]
temp.long <- temp.long[order(rev(temp.long$gender), temp.long$time), ]
rownames(temp.long) <- NULL
temp.long$incident <- seq(nrow(temp.long))
temp.long
#   age gender time W R incident
# 1  56   male  1.1 1 2        1
# 2  56   male  3.1 1 1        2
# 3  56   male  3.2 1 1        3
# 4  20 female  1.2 1 1        4
# 5  20 female  2.1 1 1        5
# 6  20 female  3.2 1 1        6

重要な場合は、さらにクリーンアップして列名と列の順序を変更できます。

于 2012-12-28T17:56:28.003 に答える