[R] quotation problem/dataframe names as function input argument.

David Winsemius dwinsemius at comcast.net
Tue Dec 23 22:16:29 CET 2008


I think you are making it too hard. See if one of these is a path to  
the answer you seek:

 > get(dframe.vec)
   V1 V2
1  1  2

#----
 > n.obs <- function(x) dim(x)
 > n.obs(get(dframe.vec))
[1] 1 2

#-------
 > ng.obs <- function(x) dim(get(x))
 > ng.obs(dframe.vec)
[1] 1 2

#-----
 > ng1.obs <- function(x) dim(get(x)[1])
 > ng1.obs(dframe.vec)
[1] 1 1

Best;
David Winsemius

On Dec 23, 2008, at 3:53 PM, Sean Zhang wrote:
> Dear R friends:
>
> Can someone help me with the following problem? Many thanks in  
> advance.
>
> # Problem Description:
> # I want to write functions which take a (character) vector of  
> dataframe
> names as input argument.
> # For example, I want to extract the number of observations from a  
> number of
> dataframes.
> # I tried the following:
>
> nobs.fun <- function (dframe.vec)
> {
>  nobs.vec <- array(NA,c(length(dframe.vec),1))
>
>  for (i in 1:length(dframe.vec))
>      {
>      nobs.vec[i] <- dim(dframe.vec[i])[1]
>      }
>
>  return(nobs.vec)
> }
>
> # To show the problem, I create a fake dataframe and store its name  
> (i.e.,
> dframe.1)
> # in a vector (i.e., dframe.vec) of length 1.
>
> # creation of fake dataframe
> dframe.1 <- as.data.frame(matrix(seq(1:2),c(1,2)))
> # store the dataframe name into a vector using c() function
> dframe.vec <- c("dframe.1")
>
> # The problem is that the following line does not work
> nobs.fun(dframe.vec)
>
> # Seems to me, the problem stems from the fact that dframe.vec[1] is
> intepreted by R as "dframe.vec" (note: it is quotated)
> # and dim("dframe.vec")[1] gives NULL.
> # Also, I realize the following line works as expected (note: dframe. 
> 1 is
> not quoted any more):
> dim(dframe.1)[1]
>
> So my question is then: how can I pass dataframe names as an input  
> argument
> for another function
> without running into the quotation mark issue above?
>
> Any hint?
>
> Thank you in advance.
> -Sean
>
> 	[[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.



More information about the R-help mailing list