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]]
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
Thank you so much! Suhaila.> Date: Mon, 7 May 2012 15:08:47 -0400 > Subject: Re: [R] Problem with Median > From: jholtman@gmail.com > To: bell_beauty12@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@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@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.[[alternative HTML version deleted]]
Hello. Thanks for the help and your reasonable explanation. I'll definitely start using 'dput' next time. Apologies for the trouble to all who helped me. Suhaila.> On 05/08/2012 06:35 PM, 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. > > Please be nicer next time. > > Hi Suhaila, > It looks like you are confused about indexing your data frame. You want > to replace the NA value in the fourth row and first column of your data > frame with a median. Say that your median value really is 78. What you > want to do is one of these: > > median_a.AC<-median(unlist(a.AC),na.rm=TRUE) > x[4,1]<-median_a.AC > x$a[4]<-median_a.AC > x[4,"a"]<-median_a.AC > > which are all pretty much equivalent. The operation of selecting one or > more values from a data frame is known as "extraction" in R. The way you > have defined a.AC results in a "list" object. If you "unlist" it, it > becomes a vector with two elements, 78 and NA. If you want the median, > you will have to use the na.rm=TRUE argument or you will get NA. Now > that you have the median, you can change the value of the NA element to > that median using one of the above commands. > > The reason that everyone was cross about sending pictures of data is > that it forces anyone who wants to help you to type in the data > manually. The "dput" function makes it easy on the helpers. > > Jim[[alternative HTML version deleted]]