Skip to content

'Apply' giving me errors

4 messages · Uwe Ligges, Kenn Konstabel, kickout

#
So i have a simple function:

bmass=function(y){
weight=y$WT*y$MSTR
return(bio)
}

And want to apply to a whole bunch of rows in my data.frame:

final1=apply(final,1,yldbu)


BUT...recieve the following error:
"Error in y$WT : $ operator is invalid for atomic vectors"


However when i try:
[1] 156.3


It gives me the correct answer....what is apply not liking in my code?

Thanks



--
View this message in context: http://r.789695.n4.nabble.com/Apply-giving-me-errors-tp3923880p3923880.html
Sent from the R help mailing list archive at Nabble.com.
#
On 21.10.2011 02:09, kickout wrote:
Since apply passes the rows as vectors into your function, not as a 
data.frame of 1 row.

I woder why you need apply() at all, since
  final$WT * final$MSTR
should do.

Uwe Ligges
#
On Fri, Oct 21, 2011 at 3:09 AM, kickout <kyle.kocak at gmail.com> wrote:
But this just returns "bio" and since an object with that name is not
defined in the function, it will be looked up in the global
environment (workspace) and if it's not there either, you will get
Error: object 'bio' not found. So even if you could fix the apply
"issue" it would still not work. But Uwe Ligges showed you don't need
apply to do what you seem to intend here, final$WT*final$MSTR should
work.

But if you do insist on apply for whatever reason then ... apply
converts X (the first argument) to a matrix so you can't use the $
operator any more. The column names are preserved though, so what you
could do is

bmass <- function(y) y["WT"] * y["MSTR"]
apply(final, 1, bmass)
What is yldbu? I suppose you meant the function you defined above?

Kenn