我知道我的問題很簡單,但不適合我。這裏是小數據集。長,如果其他循環和重新編碼在R
mark1 <- c("AB", "BB", "AB", "BB", "BB", "AB", "--", "BB")
mark2 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark3 <- c("BB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark4 <- c("AA", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark5 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark6 <- c("--", "BB", "AA", "BB", "BB", "AA", "--", "BB")
mark7 <- c("AB", "--", "AA", "BB", "BB", "AA", "--", "BB")
mark8 <- c("BB", "AA", "AA", "BB", "BB", "AA", "--", "BB")
mymark <- data.frame (mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8)
tmymark <- data.frame (t(mymark))
names (tmymark) <- c("P1", "P2","I1", "I2", "I3", "I4", "KL", "MN")
因此數據集變爲:
P1 P2 I1 I2 I3 I4 KL MN
mark1 AB BB AB BB BB AB -- BB
mark2 AB AB AA BB BB AA -- BB
mark3 BB AB AA BB BB AA -- BB
mark4 AA AB AA BB BB AA -- BB
mark5 AB AB AA BB BB AA -- BB
mark6 -- BB AA BB BB AA -- BB
mark7 AB -- AA BB BB AA -- BB
mark8 BB AA AA BB BB AA -- BB
欲分類MARK1:基於所述P1和P2對比8,並提供一個代碼,這將使得一個新的變量:
loctype <- NULL
if (tmymark$P1 == "AB" & tmymark$P2 == "AB"){
loctype = "<hkxhk>"
} else {
if (tmymark$P1== "AB" & tmymark$P2 == "BB") {
loctype = "<lmxll>"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "AB") {
loctype = "<nnxnp>"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "BB") {
loctype = "MN"
} else {
if (tmymark$P1 == "BB" & tmymark$P2 == "AA"){
loctype = "MN"
} else {
if (tmymark$P1 == "--" & tmymark$P2 == "AA"){
loctype = "NR"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "--"){
loctype = "NR"
} else {
cat ("error wrong input in P1 or P2")
}} }}}}}
在這裏,我試圖做它比較P1和P2值並生成一個新的變量。 for example,if tmymark $ P1 ==「AB」& tmymark $ P2 ==「AB」loctype應該是「」。如果不是第二個條件將是應用程序等。
這是我的錯誤消息。
Warning messages:
1: In if (tmymark$P1 == "AB" & tmymark$P2 == "AB") { :
the condition has length > 1 and only the first element will be used
2: In if (tmymark$P1 == "AB" & tmymark$P2 == "BB") { :
the condition has length > 1 and only the first element will be used
一旦LOCTYPE向量生成我要重新編碼與該變量的信息tmymark:
tmymark1 <- data.frame (loctype, tmymark)
require(car)
for(i in 2:length(tmymark)){
if (loctype = "<hkxhk>") {
tmymark[[i]] <- recode (x, "AB" = "hk", "BA" = "hk", "AA" = "hh", "BB" = "kk")
} else {
if (loctype = "<lmxll>") {
tmymark[[i]] <- recode ((x, "AB" = "lm", "BA" = "lm", "AA" = "--", "BB" = "kk")
} else {
if (loctype = "<nnxnp>") {
tmymark[[i]] <- recode ((x, "AB" = "np", "BA" = "np", "AA" = "nn", "BB" = "--")
} else {
if (loctype = "MN") {
tmymark[[i]] <- "--"
} esle {
if (loctype = "NR") {
tmymark[[i]] <- "NA"
} else {
cat ("error wrong input code")
} } }}}
上午我在正確的軌道?
編輯:預期輸出
loctype P1 P2 I1 I2 I3 I4 KL MN
mark1 <lmxmm> lm mm lm mm mm lm -- mm
mark2 <hkxhk> hk hk hh kk kk hh -- kk
mark3 <nnxnp> nn np nn -- -- nn -- --
and so on