1

I am trying to get the minimum of a a column.

The data has been split into groups using the "abbr" factor. My objective is to return the data in column 2 corresponding to the minimum in column number passed in the argument. If it helps , this is a part of the coursera R programming introductory course.

The minimum is supposed to be somewhere around 8, it shows 10.

Please help me here.

here's the link to the csv file on which i used read.csv

https://drive.google.com/file/d/0Bxkj3-FNtxqrLW14MFZCeEl6UGc/view?usp=sharing

best <- function(abbr, outvar){

    ## outcome is a dataframe consisting of a column labelled "State" (one of many)
    ## outvar is the desired column number

    statecol <- split(outcome, outcome$State) ##state is a factor which will be inputted as abbr

    dislist <- statecol[[abbr]][,2][statecol[[abbr]][, outvar] == 
                        min(statecol[[abbr]][, outvar])] ##continuation of prev line
    dislist
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
saivivek
  • 11
  • 2
  • reproducible data please – Adam Quek May 25 '17 at 07:35
  • having taken the course, and being aware of the specific question I am not sure that this is the best manner of figuring out the answer. however unless im completely out of my gourd tonight `dislist <- statecol[[abbr]][,2][statecol[[abbr]][, outvar] == ` has a syntax error. – Phi May 25 '17 at 07:40
  • dislist <- statecol[[abbr]][,2][statecol[[abbr]][, outvar] == min(statecol[[abbr]][, outvar])] – saivivek May 25 '17 at 07:45
  • I'm sorry @Phi . But I've been trying different methods to execute the code and found in the end that the min() function is giving me trouble. The line you mentioned is infact half. min() follows the ==. – saivivek May 25 '17 at 07:48
  • @AdamQuek Please let me know if this is more readable. The "outcome" is a dataframe read from a csv file with colClasses = "character". – saivivek May 25 '17 at 07:50
  • What's is `outcome`? Can't reproduce the error you are having without the a reproducible dataset. – Adam Quek May 25 '17 at 07:51
  • I see the last ']' now, but more what I am at odds with is that when you take the coursera R courses you specifically agree to not get help for homework, tests, or assignments from user forums. To get help from the coursera forum as an example you can only ask generalized questions, not specific ones. Theres no-one to enforce that here, but I'd think it was in your own interests to abide by the agreement. – Phi May 25 '17 at 07:58
  • @AdamQuek i added the link to the csv file now. – saivivek May 25 '17 at 08:02
  • What are your `abbr` and `outvar` arguments? Honestly,familiarise yourself with [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) when you are asking a question on stackoverflow. – Adam Quek May 25 '17 at 08:35
  • @AdamQuek I added the description of what abbr and outvar are. For this specific case I used the arguments, abbr = "TX" and outvar =17. The output should be "FORT DUNCAN MEDICAL CENTER" – saivivek May 25 '17 at 09:18

1 Answers1

0

In my opinion you are messing up with NA, make sure to specify na as not available and na.rm=TRUE in min..

filedata<-read.table(file.choose(),quote='"',sep=",",dec=".",header=TRUE,stringsAsFactors=FALSE, na.strings="Not Available")
   f<-function(df,abbr,outVar,na.rm=TRUE){
      outlist<-split(df,df["State"])
      tempCol<-outlist[[abbr]][outVar]
      outlist[[abbr]][,2][which(tempCol==min(tempCol,na.rm=na.rm))]
    }
    f(filedata,"AK",44)
Federico Manigrasso
  • 1,130
  • 1
  • 7
  • 11
  • Thank you for taking your time for this. I tried this and it did not work. For this specific case I used the arguments, abbr = "TX" and outvar =17. The output should be "FORT DUNCAN MEDICAL CENTER" – – saivivek May 25 '17 at 09:24
  • I tried using the which command and it returns the following error: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables Is it because i read my file as outcome <- read.csv("file-name.csv", colClasses = "character") ? – saivivek May 25 '17 at 09:25
  • I got the right result, use the command I give you to read the data. Your mistake is in reading the data – Federico Manigrasso May 25 '17 at 09:37
  • filedata<-read.table(file.choose(),quote='"',sep=",",dec=".",header=TRUE,stringsAsFactors=FALSE, na.strings="Not Available") – Federico Manigrasso May 25 '17 at 09:38
  • Thank you so much. This worked. I went through the documentation for the args of the read.table function and did not quite clearly understand the default arg stringsAsFactors = default.stringsAsFactors(). What would this have done? – saivivek May 25 '17 at 19:29
  • default command import string character as factor. This is not always what you want and usually could cause several problems. If you are satisfied by the answer pls upvote me :) Bye Federico – Federico Manigrasso May 26 '17 at 06:55
  • Thank you. I did.But my reputation score isn't high for it to be publicly displayed. – saivivek May 26 '17 at 07:48
  • It does not matter, glad to hear you solved your doubts, bye Federico – Federico Manigrasso May 26 '17 at 07:53