【问题标题】:How to print a row without header and NA values in R如何在R中打印没有标题和NA值的行
【发布时间】: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


【解决方案1】:

编辑:您的 dput 数据与您的图像不同。以下代码产生 character(0) 的结果,因为有些行没有课程推荐。在 cmets 中让我知道您希望如何处理这些问题。

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)作为输出。因为这段代码不会自己工作。问题是 data.frame 不正常。 data$student_id.Student_id 不会工作行
  • 你能发布 dput(decision_tree[1:10,]) 的结果吗?
  • 我在我的问题中添加了输出。抱歉输出混乱。
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 2020-01-11
相关资源
最近更新 更多