2013-11-25 43 views
1

我試圖用一個for循環來提取數據的子集了數據幀與R.傳遞字符串通過指數中的R

我有一點點的載體保持名稱的所有可能出現次數在該列

meter_class<-c("one_s_120","nine_s_120", "nine_s_480","fortyfive_s_120", "fortyfive_s_480")

每當我試圖通過索引引用來解決它,它失敗。無論是什麼數據子集存活(NULL值隨處可見),或R抱怨無法使用meter_class[1]

attach(meter_class[1]) 
Error in attach(meter_class[1]) : file 'one_s_120' not found 
subset(cal, cal$Form==as.character(meter_class[1])) 
[1] Test  Amps  Type  Accuracy Voltage Form 
<0 rows> (or 0-length row.names) 

又路過右邊的參數,下面是dput對datafram cal輸出:

structure(list(Test = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Burst", "ESD", "Inspection", 
"Surge"), class = "factor"), Amps = c(15, 15, 1.5, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 15, 15, 
1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25), Type = structure(c(2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L), .Label = c("Lag - 0.5", 
"Unity - Full", "Unity - Light"), class = "factor"), Accuracy = c(-0.011, 
0.012, 0.027, 0.033, 0.076, 0.006, 0.052, 0.046, -0.016, 0.021, 
0.008, 0.023, 0.034, 0.036, 0.038, 0.002, 0.012, 0.097, 0.055, 
0.093, 0.033, 0.068, 0.048, -0.016, 0.042, 0.03, 0.035, 0.041, 
0.024, 0.027, 0.004, -0.012, 0.002, 0.038, 0.084, 0.015, 0.049, 
0.045, -0.009, 0.025, 0.002, 0.029, 0.03, 0.032, 0.064, 0.011, 
0.024, 0.033, 0.054, 0.085, 0.027, 0.071, 0.059, 0.01, 0.051, 
0.012, 0.051, 0.048, 0.04, 0.051), Voltage = c(120, 120, 120, 
120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 
480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 
480, 480, 480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 480, 480, 480), Form = structure(c(3L, 3L, 3L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("45S", "9S", "i210plus" 
), class = "factor")), .Names = c("Test", "Amps", "Type", "Accuracy", 
"Voltage", "Form"), class = "data.frame", row.names = c(NA, -60L 
)) 

我知道如果你知道如何去做,這是一件很簡單的事情......任何人都可以照亮自己的路?

謝謝!

+2

您好,歡迎來SO!如果你發佈一個[最小的,可重現的例子],那麼人們就可以更容易地幫助你解決問題了(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)。此外,發佈任何錯誤消息 - 他們往往是相當豐富。最後,避免使用'attach'。見例如'attach「,」Good Practice「以及論壇中關於'attach'引起的潛在不愉快意外的幾個主題。 – Henrik

+0

感謝您的迴應!我接受了您的建議並更新了代碼以包含錯誤消息。 – testname123

+0

該帖子已更新。感謝您尋求幫助的方向。 – testname123

回答

1

似乎在您的數據框中「表格」中沒有「meter_class」的值。

unique(df$Form) 
# [1] i210plus 9S  45S 

meter_class %in% unique(df$Form) 
# [1] FALSE FALSE FALSE FALSE FALSE 

剛剛嘗試兩種形式的子集,在數據使用的「表」實際存在的價值:

subset(df, Form == "9S") 
df[df$Form == "9S", ] 

我也注意到,您希望「拉數據子集了數據幀的」 。不知道完整的故事和你這樣做的目標,但請注意,有許多函數允許你對數據的子集進行計算,繪圖或其他。

更新如下評論
您可以通過子集邏輯條件與邏輯運算符組合數據幀(見例如?Extract?&

meter_class <- c("i210plus", "9S", "45S") 
df[df$Form == "9S" & df$Voltage == 120, ] 
# or 
subset(df, Form == "9S" & Voltage == 120)