我不認爲我能找到類似版本的這個問題張貼,因爲我覺得這是一個相對獨特的問題,但請指出我在正確的方向,如果我錯了。我用下面的載體,我需要轉換成數據幀的工作:將矢量轉換爲字符串子集中的2列數據框R
myvec = structure(c(1.03, 2.3, -1.2, -0.09, -0.31, -0.51, 3.4, 3, 0.07,
0.02, 1.05, -0.02, 2.03), .Names = c("Intercept", "DEF-1017",
"DEF-1025", "DEF-103", "DEF-1043", "DEF-1046", "DEF-1048", "DEF-1076",
"OFF-1017", "OFF-1025", "OFF-103", "OFF-1046", "OFF-1076"))
head(myvec)
Intercept DEF-1017 DEF-1025 DEF-103 DEF-1043 DEF-1046
1.03 2.30 -1.20 -0.09 -0.31 -0.51
該載體應該具有進攻(OFF)和防禦(DEF)的係數爲7個不同的用戶(用戶1017,1025,103, 1043,1046,1048,1076),但是對於兩個用戶缺少冒犯性係數。我需要將這個數據轉換成4欄(防守ID,進攻ID,防守係數,進攻係數)的數據框。更具體地講,我希望得到以下數據框中,佔以這種方式爲遺漏值:
mydf = structure(list(DEFID = c("DEF-1017", "DEF-1025", "DEF-103", "DEF-1043",
"DEF-1046", "DEF-1048", "DEF-1076"), OFFID = c("OFF-1017", "OFF-1025",
"OFF-103", NA, "OFF-1046", NA, "OFF-1076"), DEFVAL = c(2.3, -1.2,
-0.09, -0.31, -0.51, 3.4, 3), OFFVAL = c(0.07, 0.02, 1.05, NA,
-0.02, NA, 2.03)), .Names = c("DEFID", "OFFID", "DEFVAL", "OFFVAL"
), row.names = c(NA, -7L), class = "data.frame")
mydf
DEFID OFFID DEFVAL OFFVAL
1 DEF-1017 OFF-1017 2.30 0.07
2 DEF-1025 OFF-1025 -1.20 0.02
3 DEF-103 OFF-103 -0.09 1.05
4 DEF-1043 <NA> -0.31 NA
5 DEF-1046 OFF-1046 -0.51 -0.02
6 DEF-1048 <NA> 3.40 NA
7 DEF-1076 OFF-1076 3.00 2.03
截距值被丟棄/不列入表中,和其他一切被格式化爲預計會。任何幫助,將不勝感激,謝謝!