【问题标题】:Error with arrange in dplyr: incorrect size(1) at position1, expecting: 3947dplyr 中的排列错误:位置 1 的大小(1)不正确,预期:3947
【发布时间】:2020-05-07 14:06:57
【问题描述】:

# 我已经尝试了好几天了。没运气。每个#vector 的长度为 3947,我在 as.numeric、#as.data.frame 和输入之间进行了更改。欢迎提出任何建议。

#pulling data from outcomes on hospitals, with eventual attempt to return ranking based on this.
rankall<-function(outcome,num) {
  library(datasets)
  library(plyr)
  library(dplyr)
  data<-read.csv("outcome-of-care-measures.csv", na.strings="Not Available",
                 stringsAsFactors=FALSE)

  if (outcome == "pneumonia"){
    column_index<-23
  } else if (outcome == "heart attack") {
    column_index<-11
  } else if (outcome == "heart failure") {
    column_index<-17
  } else {
    stop("invalid outcome")
  } 

  data2<-cbind(data[,2],data[,7],data[,column_index])
  data2<-na.omit(data2)
  colnames(data2)<-c("hospital", "state", outcome)
  order1<-data2[order(as.numeric(data2[,outcome]), data2[,"hospital"]),]
  numrows<-nrow(order1)
  as.data.frame(order1) %>%
    arrange(outcome, .by_group=TRUE)

  print(class(data2[,"hospital"]))
  print(class(data2[,2]))
  print(class(data2[,3]))
}  

【问题讨论】:

  • 能否分享dput(data2)的输出,如果data2很大,分享dput(head(data2))
  • 奇数。 dput(data2)输出大量数据,dput(head(data2))报错:Error in parse(text = x) : :1:7: unexpected symbol 1: heart failure
  • 这是出乎意料的,试试这个:dput(data2[1:5,])
  • 同样的错误。重新运行 dput(data2),错误出现在数据转储的末尾。

标签: r dplyr


【解决方案1】:

你需要将结果变成一个符号,它现在被视为一个字符串。另见this。 要纠正,请执行以下操作:

library(dplyr)

data2 = data.frame(x=runif(100),
hospital=sample(LETTERS,100,replace=TRUE),
"heart attack" = rbinom(100,1,0.5),
"pneumonia" = rbinom(100,1,0.5),check.names=FALSE)

outcome = "pneumonia"

data2 %>% arrange(outcome, .by_group=TRUE)
Error: incorrect size (1) at position 1, expecting : 100

data2 %>% arrange(!!sym(outcome), .by_group=TRUE)

              x hospital heart attack pneumonia
1   0.471584775        B            0         0
2   0.907479862        S            1         0
3   0.141569308        Q            0         0
4   0.511258807        A            0         0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-18
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2015-04-24
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多