简体   繁体   中英

How do I subset columns from on data

 rankhospital <- function(state = factor(), outcome = factor(), num = factor()) { #read data caremeasures <- read.csv("D:/data science specialization/course stuff/rprogw3/outcome-of-care-measures.csv", na.strings = "NA", stringsAsFactors = FALSE) #separate required columns requiredOutcomes <- caremeasures[11, 17, 23] #assign columns names names(requiredOutcomes[3]) <- "heart attack" names(requiredOutcomes[4]) <- "heart failure" names(requiredOutcomes[5]) <- "pneumonia" arrangedData <- order(requiredOutcomes[caremeasures$State == state, c(caremeasures$Hospital.Name, outcome)]) if (num == "best"){ result <- arrangedData[1, 1] return(result) } else if (num == "worst"){ result <- arrangedData[nrow(arrangedData[,1]),1] return(result) } else result <- arrangedData[num, 1] return(result) }

This code is supposed to return the name of a single hospital that corresponds to the inputs given to the function, yet I'm getting an error stating:

Error in requiredOutcomes[caremeasures$State == state, c(caremeasures$Hospital.Name,  : 
  incorrect number of dimensions

I don't have your data, so I'll project what I think is leading up to the problem.

The reference to caremeasures[11, 17, 23] is likely not doing what you need, and it is therefore returning something you aren't expecting. Try it with caremeasures[,c(11, 17, 23)] .

I'll try to show what's going on using mtcars :

requiredOutcomes <- mtcars[1,2,3]
requiredOutcomes
# [1] 6
requiredOutcomes[1,2]
# Error in requiredOutcomes[1, 2] : incorrect number of dimensions

Because mtcars is a data.frame , your [ indexing uses [.data.frame under the hood. This translates something like

# equivalent
mtcars[1, 2]
`[.data.frame`(mtcars, 1, 2)

The "arguments" (yes, it's just a regular function) are:

str(formals(`[.data.frame`))
# Dotted pair list of 4
#  $ x   : symbol 
#  $ i   : symbol 
#  $ j   : symbol 
#  $ drop: language if (missing(i)) TRUE else length(cols) == 1

which means that your 11, 17, 23 arguments are effectively

`[.data.frame`(requireOutcomes, 11, 17, 23)

which is applied to the arguments as

`[.data.frame`(x = requireOutcomes, i = 11, j = 17, drop = 23)

Okay, so x= makes sense (the data). i= gives your row selection (11), and j= gives the column selection. However, when R expected a logical , then anything that is not zero is considered TRUE , so this is effectively

`[.data.frame`(x = requireOutcomes, i = 11, j = 17, drop = TRUE)

which completely loses your intent (I suspect) by returning a scalar (single value, a vector of length 1 in R). Side note: had you used 0 or FALSE , then you would have returned a data.frame with 1 row and 1 column.

Here's a method for debugging what's going on so that you are able to find this for yourself next time.

myfunc <- function(x) {
  res <- x[1,2,3]
  return(res[1:3,])
}
myfunc(mtcars)
# Error in res[1:3, ] (from #3) : incorrect number of dimensions

Okay, we see the same error. We'll use debug(myfunc) (whatever your function name is), though you can achieve similar results by placing browser() at a specific place within your function.

debug(myfunc)
myfunc(mtcars)
# debugging in: myfunc(mtcars)
# debug at #1: {
#     res <- x[1, 2, 3]
#     return(res[1:3, ])
# }
# Browse[2]> 

We're now in R's debugger, giving us step execution tracing. Typing n executes the n ext line; you can read more of the commands with ?browser .

n
# debug at #2: res <- x[1, 2, 3]
# Browse[2]> 
n
# debug at #3: return(res[1:3, ])
# Browse[2]> 
res
# [1] 6

("debug at" shows the next line to be executed, so we have not yet run return(...) .) With this, we can see that res -- which we think should be a data.frame -- is just a single number. Huh. Now look back at the code and figure out what happened. To me (and in this simple example), it's clearly x[1,2,3] that's a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM