【发布时间】:2017-04-05 23:05:56
【问题描述】:
我有一个名为 decision_tree 的数据框。我需要使用条件打印数据框中的一行。例如:Student_id == 100。
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")
数据框看起来像:
最后一个字段是数字,其他字段是 7 或 9 个级别的因子。
即使我将 colnames 设置为 NULL。最后一列将有一个 Student_id。前 5 个列名将从 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")
【问题讨论】:
-
你能做到吗?:selected_row
-
@EvanFriedland 是的,这行得通。但它也会打印带有 na 值的标题和列。如何像列表一样打印它?只是单元格值。一个低于另一个
-
你能粘贴你想要的输出吗?我猜这里。
-
@EvanFriedland 如果您参考上图,那么对于 student_id = 55,我需要 F20RS、F20MC、F20GP,如果 student_id = 101 则只有 F20DP。我只需要一个低于另一个的单元格值。不需要列标题。
-
@EvanFriedland 任何解决方案都可以,优化不是问题。如果转换为向量或矩阵可以完成这项工作,我会非常高兴。
标签: r