lundi 24 août 2015

R- finding the best, worst, or number in a data set

New to R and programming in general. I want to make a function that finds the rank of a place given season and state and return that place name. I've edited this to be more of a learning exercise for me

I've written this: rankh<-function(state,season, num="best"){

##Read season data
    dataset<- read.csv("outcome-of-care-measures.csv", na.strings="Not Available")

## check that state and season are valid entries
    seasons_valid<-c("spring"=11,"summer"=17,"fall"=23)
    pick <- seasons_valid[season]

    if(! state %in% dataset$State) stop("invalid state")
    if(! season %in% names(seasons_valid)) stop("invalid season")

## return place in state with given rank (num) 
    df <- dataset[dataset$State==state,]
    ord <- order(df[,pick])


    if (num=="best"){
        place <- pick[which.min(pick[[ord]])]
        return (place$Place.name)

    } else if (num=="worst"){
        place<- pick[which.max(pick[[ord]])]
        return (place$Place.name)
    } else if(is.numeric(num)){
        place<- pick[num(pick[[ord]])]
        return (place$Place.name)
        }}

Where am I going wrong?

Aucun commentaire:

Enregistrer un commentaire