2017-04-05 57 views
0

我有一個名爲decision_tree的數據框。我需要用條件從數據框中打印一行。例如:Student_id == 100.如何在R中打印無標題和NA值的行

selected_row <- filter(decision_tree, Student_id=100) 

但是我收到了一些奇怪的錯誤。

Error: Each variable must be a 1d atomic vector or list. 
Problem variables: 'Student_id' 

decision_tree <- plyr::ldply(Recommendations, rbind) 
decision_tree$Student_id <- select(r_df, Student_id) 
colnames(decision_tree) <- c("Recommended Course 1","Recommended Course 2","Recommended Course 3","Recommended Course 4","Recommended Course 5","Student_id") 

數據幀看起來像:

data frame

最後場是數字和其它字段是與7級或9的水平的因素。

即使我將colnames設置爲NULL。最後一列將有一個Student_id。前5列名稱將從1到5

結構(列表(Recommended Course 1 =結構(c(NA,NA,NA, 5L,NA,NA,NA,NA,8L,2L),。標籤= c(「p_F20BC」,「p_F20DL」, 「p_F20DP」,「p_F20DV」,「p_F20GP」,「p_F20MA」,「p_F20MC」,「p_F20RO」, 「p_F20RS」),class =「factor」),Recommended Course 2 =結構(c(NA, NA,9L,NA,NA,NA,2L,7L),標籤= c(「p_F20BC」,「p_F20DL」, 「p_F20DP」,「p_F20DV」,「p_F20GP 「,」p_F20MA「,」p_F20MC「,」p_F20RO「, 」p_F20RS「),class =」factor「),Recommended Course 3 = structure(c(NA, NA,NA,NA,NA,NA,NA,NA, 1L,3L),。標籤= c(「p_F20BC」,「p_F20DL」, 「p_F20DP 」, 「p_F20GP」, 「p_F20MC」, 「p_F20RO」),類= 「因子」), Recommended Course 4 =結構(C(NA_integer_,NA_integer_, NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_, NA_integer_,NA_integer_,NA_integer_ ),標籤=「p_F20BC」,class =「factor」), Student_id = structure(list(Student_id = c(55L,68L,70L, 99L,100L,101L,103L,105L,106L,107L) .Names =「Student_id」,row.names = c(NA, 10L),class =「data.frame」)),.Names = c(「推薦課程1」, 「推薦課程2」,「 3「,」Recommended Course 4「, 」Student_id「),row.names = c(NA,10L),class =」data.frame「)

+1

你能不能做到?:selected_row < - 構造判定[其中(判定樹[ 「student_id數據」] == 100)] –

+0

@EvanFriedland沒錯,就是工作。但它也會打印帶有na值的標題和列。如何像列表一樣打印?只是單元格的價值。一個在另一個下方 –

+0

你可以粘貼你想要的輸出嗎?我在這裏猜測。 –

回答

1

編輯:您的輸入數據與您的圖像不同。以下代碼會生成字符(0)的結果,因爲存在沒有課程建議的行。在評論中讓我知道你想如何處理這些。

decision_tree <- structure(list("Recommended Course 1" = structure(c(NA, NA, NA, 5L, NA, NA, NA, NA, 8L, 2L), .Label = c("p_F20BC", "p_F20DL", "p_F20DP", "p_F20DV", "p_F20GP", "p_F20MA", "p_F20MC", "p_F20RO", "p_F20RS"), class = "factor"), "Recommended Course 2" = structure(c(NA, NA, NA, 9L, NA, NA, NA, NA, 2L, 7L), .Label = c("p_F20BC", "p_F20DL", "p_F20DP", "p_F20DV", "p_F20GP", "p_F20MA", "p_F20MC", "p_F20RO", "p_F20RS"), class = "factor"), "Recommended Course 3" = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 1L, 3L), .Label = c("p_F20BC", "p_F20DL", "p_F20DP", "p_F20GP", "p_F20MC", "p_F20RO"), class = "factor"), "Recommended Course 4" = structure(c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = "p_F20BC", class = "factor"), Student_id = structure(list(Student_id = c(55L, 68L, 70L, 99L, 100L, 101L, 103L, 105L, 106L, 107L)), .Names = "Student_id", row.names = c(NA, 10L), class = "data.frame")), .Names = c("Recommended Course 1", "Recommended Course 2", "Recommended Course 3", "Recommended Course 4", "Student_id"), row.names = c(NA, 10L), class = "data.frame") 


recommend <- function(StudentID){ 
    courses <- grep("Course", colnames(decision_tree)) 
    id <- grep("id", colnames(decision_tree)) 
    rows <- which(decision_tree[,id] == StudentID) 
    x <- decision_tree[rows,courses][!is.na(decision_tree[rows,courses])] 
    if(length(x) == 0) { 
    paste("No course to recommend") 
    } else { 
    x 
    } 
} 
recommend(99) 
    "p_F20GP" "p_F20RS" 

for(i in 1:nrow(decision_tree)){ 
    print(recommend(decision_tree$Student_id[i,])) 
} 
[1] "No course to recommend" 
[1] "No course to recommend" 
[1] "No course to recommend" 
[1] "p_F20GP" "p_F20RS" 
[1] "No course to recommend" 
[1] "No course to recommend" 
[1] "No course to recommend" 
[1] "No course to recommend" 
[1] "p_F20RO" "p_F20DL" "p_F20BC" 
[1] "p_F20DL" "p_F20MC" "p_F20DP" 
+0

它不會工作,我得到邏輯(0)作爲輸出。因爲這段代碼不會自行工作。問題是data.frame不能正常工作。數據$ student_id數據。Student_id不會工作行< - 哪個(data $ Student_id.Student_id == StudentID) –

+0

您可以發佈dput(decision_tree [1:10,])的結果嗎? –

+0

我在我的問題中添加了輸出。抱歉雜亂的輸出。 –