i have dataframe called decision_tree
. need print 1 of row data frame condition. example: student_id == 100.
selected_row <- filter(decision_tree, student_id=100)
but getting weird error.
error: each variable must 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 looks like:
last field numeric , other fields factors 7 or 9 levels.
even if set colnames null. last column have student_id. , first 5 column names 1 5
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")
edit: dput data different image. following code produces results of character(0) because there rows without course recommendations. in comments let me know how handle these.
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 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 recommend" [1] "no course recommend" [1] "no course recommend" [1] "p_f20gp" "p_f20rs" [1] "no course recommend" [1] "no course recommend" [1] "no course recommend" [1] "no course recommend" [1] "p_f20ro" "p_f20dl" "p_f20bc" [1] "p_f20dl" "p_f20mc" "p_f20dp"
Comments
Post a Comment