If you want to pass a data frame (not just its name) plus (some) column names
to your function, the easiest way is to put the _quoted_ names in a vector, as
previously posted. For example
f1 <- function(dfrm, colnames) {
print(dfrm[,colnames]) #or, if you like, for(nn in colnames)
Simply quote the colnames in the call, i.e.:
demo("a", "b", df)
Best,
Uwe Ligges
On 02.06.2019 03:43, Sorkin, John wrote:
Colleagues,
Despite Bert having tried to help me, I am still unable to perform a simple act
with a function. I want to pass the names of the columns of a dataframe along
John,
I believe the pieces you are missing are filed under 'computing on the
language', 'passing unevaluated objects', and 'language objects'.
Forgive me if I belabor things you already know.
lm, transform, and many other functions do their "magic" by operating on
language objects.
You
PS: lm records a copy of the call in its result, but has no other use
for any name the data frame may have had.
On Sun, 2 Jun 2019 at 14:45, Richard O'Keefe wrote:
> You can find the names of the columns of a dataframe using
> colnames(my.df)
> A dataframe is a value just as much as a number
You can find the names of the columns of a dataframe using
colnames(my.df)
A dataframe is a value just as much as a number is, and as such,
doesn't _have_ a name. However, when you call a function in R,
the arguments are not evaluated, and their forms can be recovered,
just as "plot" does. In
Hint:
> all.vars(a)
Error in all.vars(a) : object 'a' not found
> all.vars(quote(a)) ## protects "a" from evaluation; quote(a) is a symbol
expression
[1] "a"
> all.vars(~a) ## a formula expression
[1] "a"
-- Bert
On Sat, Jun 1, 2019 at 6:43 PM Sorkin, John
wrote:
> Colleagues,
>
> Despite
Colleagues,
Despite Bert having tried to help me, I am still unable to perform a simple act
with a function. I want to pass the names of the columns of a dataframe along
with the name of the dataframe, and use the parameters to allow the function to
access the dataframe and modify its
Depends on how you want to specify variables. You are not clear (to me) on
this. But, for instance:
demo <- function(form,df)
{
av <- all.vars(form)
df[,av]
}
demo(~a+b, df)
demo(a~b,df)
?all.vars, ?all.names for details
Bert Gunter
On Wed, May 29, 2019 at 7:33 PM Sorkin, John
wrote:
Bert,
Thank you for your reply. You are correct that your code will print the
contents of the data frame. While it works, it is not as elegant as the lm
function. One does not have to pass the independent and dependent variables to
lm In parentheses.
Fit1<-lm(y~x,data=mydata)
None of the
Basically, huh?
> df <- data.frame(a = 1:3, b = letters[1:3])
> nm <- names(df)
> print(df[,nm[1]])
[1] 1 2 3
> print(df[,nm[2]])
[1] a b c
Levels: a b c
This can be done within a function, of course:
> demo <- function(df, colnames){
+print(df[,colnames])
+ }
> demo(df,c("a","b"))
a b
1
Thanks to several kind people, I understand how to use
deparse(substitute(paramter)) to get as text strings the arguments passed to an
R function. What I still can't do is put the text strings recovered by
deparse(substitute(parameter)) back together to get the columns of a dataframe
passed to
11 matches
Mail list logo