Reputation: 2609
I have a dataframe called decision_tree
. I need to print one of the row from the data frame with a condition. for example: Student_id == 100.
selected_row <- filter(decision_tree, Student_id=100)
But I am getting some weird error.
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 looks like:
last field is numeric and other fields are factors with 7 or 9 levels.
Even if I set colnames to NULL. last column will have a Student_id. and first 5 column names would be from 1 to 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")
Upvotes: 0
Views: 1920
Reputation: 3194
Edit: Your dput data is different from your image. The following code produces results of character(0) because there are rows without course recommendations. In the comments let me know how you would like to 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 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"
Upvotes: 1