[R] Problem with Median

David Winsemius dwinsemius at comcast.net
Tue May 8 14:37:27 CEST 2012


On May 8, 2012, at 4:35 AM, Suhaila Haji Mohd Hussin wrote:

> Hello.
>
> Sorry if that's considered laziness as I've just learnt R and didn't  
> know how important it is to do dput for all problems.
>
> If I was truly lazy then I wouldn't even bother to sign up here and  
> ask questions.

I didn't say you were lazy. I implied that you were being  
insufficiently lazy. It would have been much easier  (had you read the  
Posting Guide and been properly lazy) to use either dump or dput to  
post machine readable versions of your data than to make a jpg and  
post it on an external website.

  I considered, but then decided that it was probablyunnecessary,  
offering a link to what is a fairly famous piece by Larry Wall  
explaining that one _should_ be lazy if one wants to be a good  
programmer. The Three Virtues: Laziness, Impatience, Hubris:

http://c2.com/cgi/wiki?LazinessImpatienceHubris

-- 
David.

>
> Please be nicer next time.
>
> Suhaila.
>
> > CC: r-help at r-project.org
> > From: dwinsemius at comcast.net
> > To: bell_beauty12 at hotmail.com
> > Subject: Re: [R] Problem with Median
>
> > Date: Mon, 7 May 2012 20:25:24 -0400
> >
> >
> > On May 7, 2012, at 5:16 PM, Suhaila Haji Mohd Hussin wrote:
> >
> > >
> > > Hello.
> > > Now the median is solved but then I'm still figuring out how to  
> put
> > > the updated column back to replace the original column of the  
> whole
> > > data. I'll show you what I meant:
> > > Continuing from the previous commands you guys helped out I
> > > continued as followed:
> > > Original Data: http://i1165.photobucket.com/albums/q585/halfpirate/data.jpg
> > >
> > > Before column a: http://i1165.photobucket.com/albums/q585/halfpirate/1.jpg
> >
> > That fine for sending pictures to the family but NOT for  
> communicating
> > R code.
> > >
> > > 1) a.AC [is.na(a.AC)] <- median(a.AC$a, na.rm= TRUE)
> > > 2) a.ACAfter column a: http://i1165.photobucket.com/albums/q585/halfpirate/2.jpg
> > > 3) data$a <- a.ACGives me error that the original data has 6 rows
> > > yet the one I'm replacing is 1 row.
> > > I understand the error and I don't want to completely replace the
> > > column if I really want to.
> >
> > You were not attempting to replace the entire column, but you were
> > trying to replace 6 entries. So why not... :
> >
> > a.AC [is.na(a.AC)] <- rep( median(a.AC$a, na.rm= TRUE) , 6)
> >
> >
> > > Basically I'm expecting the result to be like this. After you
> > > compare the original data, the final answer should be like thishttp://i1165.photobucket.com/albums/q585/halfpirate/3.jpg
> >
> > What part of "I can't do anything with your jpeg to answer your
> > questions about your dataframe" was difficult to understand? We do  
> NOT
> > want pictures from which we need to repeat data entry that you have
> > already done. The first virtue of a programmer being laziness.
> > Console output is also very ambiguous. Learn to use str() and dput()
> > or dump().
> >
> > ?dput
> > ?dump
> >
> > And run the examples. please.
> >
> > --
> > David.
> >
> >
> > > Please help.Suhaila
> > > From: bell_beauty12 at hotmail.com
> > > To: jholtman at gmail.com
> > > CC: r-help at r-project.org
> > > Subject: RE: [R] Problem with Median
> > > Date: Tue, 8 May 2012 07:22:19 +1200
> > >
> > > Thank you so much!
> > > Suhaila.
> > >
> > >> Date: Mon, 7 May 2012 15:08:47 -0400
> > >> Subject: Re: [R] Problem with Median
> > >> From: jholtman at gmail.com
> > >> To: bell_beauty12 at hotmail.com
> > >>
> > >> Your problem is that a.AC is a dataframe:
> > >>
> > >>
> > >>> x <- read.table(text = " a b c class
> > >> + 1 12 0 90 A-B
> > >> + 2 3 97 11 A-B
> > >> + 3 78 NA 123 A-C
> > >> + 4 NA NA 12 A-C
> > >> + 5 8 33 2 A-B
> > >> + 6 12 NA 0 A-D", header = TRUE)
> > >>> a.AC <- subset(x, class == "A-C", select = a)
> > >>> # same error
> > >>> median(a.AC)
> > >> Error in median.default(a.AC) : need numeric data
> > >>
> > >>> # now look a the structure of a.AC (its a dataframe)
> > >>> str(a.AC)
> > >> 'data.frame': 2 obs. of 1 variable:
> > >> $ a: int 78 NA
> > >>> # now do it right
> > >>> median(a.AC$a)
> > >> [1] NA
> > >>> median(a.AC$a, na.rm = TRUE)
> > >> [1] 78
> > >>
> > >>
> > >>
> > >> On Mon, May 7, 2012 at 2:52 PM, Suhaila Haji Mohd Hussin
> > >> <bell_beauty12 at hotmail.com> wrote:
> > >>>
> > >>> Hello.
> > >>> I'm trying to compute median for a filtered column based on  
> other
> > >>> column but there was something wrong. I'll show how I did step  
> by
> > >>> step.
> > >>> Here's the data:
> > >>> a b c class
> > >>>
> > >>> 1 12 0 90 A-B2 3 97 11 A-B3 78 NA
> > >>> 123 A-C4 NA NA 12 A-C5 8 33 2 A-B6
> > >>> 12 NA 0 A-D
> > >>> On the command I typed:
> > >>> 1) data = read.csv("data.csv")
> > >>>
> > >>> 2) a.AC <- subset(data, class == "A-C", select = a)
> > >>> 3) median(a.AC)Error in median.default(a.AC) : need numeric data
> > >>> 4) is.numeric(a.AC)FALSE
> > >>> 5) as.numeric(a.AC)Error: (list) object cannot be coerced to  
> type
> > >>> 'double'
> > >>> How can I fix this? Please help.
> > >>> Cheers,Suhaila
> > >>> [[alternative HTML version deleted]]
> > >>>
> > >>> ______________________________________________
> > >>> R-help at r-project.org mailing list
> > >>> https://stat.ethz.ch/mailman/listinfo/r-help
> > >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > >>> and provide commented, minimal, self-contained, reproducible  
> code.
> > >>
> > >>
> > >>
> > >> --
> > >> Jim Holtman
> > >> Data Munger Guru
> > >>
> > >> What is the problem that you are trying to solve?
> > >> Tell me what you want to do, not how you want to do it.
> > >
> > >
> > > I might be silly but if I was going to type in dput() then how
> > > should I send the data over here?
> > > Instead, I've just uploaded the image online, you can access it  
> via
> > > the link below.
> > > http://i1165.photobucket.com/albums/q585/halfpirate/data.jpg
> > >
> > >> Date: Mon, 7 May 2012 14:55:24 -0400
> > >> Subject: Re: [R] Problem with Median
> > >> From: sarah.goslee at gmail.com
> > >> To: bell_beauty12 at hotmail.com
> > >> CC: r-help at r-project.org
> > >>
> > >> Please use dput() to give us your data (eg dput(data) ) rather  
> than
> > >> simply pasting it in.
> > >>
> > >> Sarah
> > >>
> > >> On Mon, May 7, 2012 at 2:52 PM, Suhaila Haji Mohd Hussin
> > >> <bell_beauty12 at hotmail.com> wrote:
> > >>>
> > >>> Hello.
> > >>> I'm trying to compute median for a filtered column based on  
> other
> > >>> column but there was something wrong. I'll show how I did step  
> by
> > >>> step.
> > >>> Here's the data:
> > >>> a b c class
> > >>>
> > >>> 1 12 0 90 A-B2 3 97 11 A-B3 78 NA
> > >>> 123 A-C4 NA NA 12 A-C5 8 33 2 A-B6
> > >>> 12 NA 0 A-D
> > >>> On the command I typed:
> > >>> 1) data = read.csv("data.csv")
> > >>>
> > >>> 2) a.AC <- subset(data, class == "A-C", select = a)
> > >>> 3) median(a.AC)Error in median.default(a.AC) : need numeric data
> > >>> 4) is.numeric(a.AC)FALSE
> > >>> 5) as.numeric(a.AC)Error: (list) object cannot be coerced to  
> type
> > >>> 'double'
> > >>> How can I fix this? Please help.
> > >>> Cheers,Suhaila
> > >>
> > >>
> > >> --
> > >> Sarah Goslee
> > >> http://www.functionaldiversity.org
> > >
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > David Winsemius, MD
> > West Hartford, CT
> >

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list