Hi All, I want to enclose with() in a function mean_on_element. Obviously, it is not working. The problem is how to specify the element name with a function body. Does anybody have any suggestion? Thanks!> data=list(x=1:10) > with(data, mean(x))[1] 5.5> > mean_on_element=function(data, elem_name) {+ with(data, mean(elem_name)) + }> mean_on_element(data, 'x')[1] NA Warning message: In mean.default(elem_name) : argument is not numeric or logical: returning NA -- Tom
Hi Tom,
What exactly is this function supposed to do? Your immediate problem is that
you are passing it a string "x" and asking for a mean of the string
"x"
(hence complaints that it's not numeric) but I'm a little confused as to
what this is supposed to do when it works.
If you just want the mean of the list element named "x", this should
do:
mean(data[["x"]])
If more generally you need to set up an environment, perhaps attach will
work -- but if you intend to write a function, why not just subset the list
as needed?
mean_on_element2 <- function(data,elem_name) {
r = mean(data[[elem_name]])
return(r)
}
Now you can access list elements by their index or by a string containing
the name.
Hope this helps,
Michael Weylandt
On Mon, Aug 8, 2011 at 6:12 PM, thmsfuller066@gmail.com <
thmsfuller066@gmail.com> wrote:
> Hi All,
>
> I want to enclose with() in a function mean_on_element. Obviously, it
> is not working. The problem is how to specify the element name with a
> function body. Does anybody have any suggestion? Thanks!
>
> > data=list(x=1:10)
> > with(data, mean(x))
> [1] 5.5
> >
> > mean_on_element=function(data, elem_name) {
> + with(data, mean(elem_name))
> + }
> > mean_on_element(data, 'x')
> [1] NA
> Warning message:
> In mean.default(elem_name) :
> argument is not numeric or logical: returning NA
>
>
> --
> Tom
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
Hi:
Here are a couple of ways; there may well be better ones.
# (1) Use the get() function:
mean_on_element=function(data, elem_name) {
with(data, mean(get(elem_name)))
}
mean_on_element(data, 'x')
# (2) Lose 'with' and use subscripting instead:
mean_on_element=function(data, elem_name) {
mean(data[[elem_name]])
}
mean_on_element(data, 'x')
Since 'x' is quoted in the function call, you need to use code that
can convert the string 'x' to extracting the data object with name x.
HTH,
Dennis
On Mon, Aug 8, 2011 at 3:12 PM, thmsfuller066 at gmail.com
<thmsfuller066 at gmail.com> wrote:> Hi All,
>
> I want to enclose with() in a function mean_on_element. Obviously, it
> is not working. The problem is how to specify the element name with a
> function body. Does anybody have any suggestion? Thanks!
>
>> data=list(x=1:10)
>> with(data, mean(x))
> [1] 5.5
>>
>> mean_on_element=function(data, elem_name) {
> + ? with(data, mean(elem_name))
> + }
>> mean_on_element(data, 'x')
> [1] NA
> Warning message:
> In mean.default(elem_name) :
> ?argument is not numeric or logical: returning NA
>
>
> --
> Tom
>
> ______________________________________________
> 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.
>