Skip to content

Erroneous Column Removing Result From User-Defined Function.....

3 messages · Levent TERLEMEZ, Jeff Newmiller, Adams, Jean

#
You have pasted together a string, and are now trying to treat it as a 
data frame? I am not surprised at this error message.

Please take heed of this warning: you are in Circle 6 of the R Inferno 
[1], and would benefit greatly from learning to put your 
multiple-but-similarly-structured data into a list and returning the list 
from the function instead of mucking around in the global environment. [2]

[1] http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

[2] Example:

MyFunction <- function ( dyolu, dadi, dsayi ) {
    # integer sequence up to dsayi
    dsayiseq <- seq.int( dsayi )
    # vector of names
    ad <- paste( dadi, dsayiseq, sep="" )
    # vector of filenames based on ad
    yol <- paste( dyolu, ad, ".csv", sep="" )
    # generate list of data frames, one filename at a time
    result <- lapply( yol
                    , function( yoli ) {
                        # avoid using "T" and "F"... they are variables
                        # that can be re-defined... instead use TRUE and
                        # FALSE
                        dta <- read.csv( yoli, sep=";", dec=","
                                       , header=TRUE )
                        # note that accessing columns by position is a
                        # brittle approach...
                        #  if a new column is inserted, this code will
                        #  break. Would be better to use column names
                        dta <- dta[,-12]
                        # dta is a local variable... as the result of this
                        # inner function, its value is stored in the result
                        # list
                        dta
                      }
                    )
    # name the elements of the list for easier access
    names( result ) <- ad
    result
}

# make example reproducible
set.seed( 42 )
# create fake files to test code with
for ( i in 1:5 ) {
    m <- matrix( runif( 150 ), nrow=10 )
    colnames( m ) <- paste0( "Col", 1:15 )
    samp <- data.frame( key=1:10, m )
    write.csv2( samp, paste0( "Fnameprefix", "Dtaprefix", i, ".csv" )
              , row.names=FALSE )
}

dtaList <- MyFunction( "Fnameprefix", "Dtaprefix", 5 )
List of 5
  $ Dtaprefix1:'data.frame':	10 obs. of  15 variables:
  $ Dtaprefix2:'data.frame':	10 obs. of  15 variables:
  $ Dtaprefix3:'data.frame':	10 obs. of  15 variables:
  $ Dtaprefix4:'data.frame':	10 obs. of  15 variables:
  $ Dtaprefix5:'data.frame':	10 obs. of  15 variables:
'data.frame':	10 obs. of  15 variables:
  $ key  : int  1 2 3 4 5 6 7 8 9 10
  $ Col1 : num  0.719 0.324 0.779 0.394 0.679 ...
  $ Col2 : num  0.935 0.55 0.602 0.197 0.535 ...
  $ Col3 : num  0.73 0.412 0.414 0.48 0.427 ...
  $ Col4 : num  0.918 0.863 0.317 0.259 0.742 ...
  $ Col5 : num  0.1947 0.7841 0.1289 0.1291 0.0723 ...
  $ Col6 : num  0.885 0.517 0.852 0.443 0.158 ...
  $ Col7 : num  0.542 0.6499 0.3364 0.0609 0.4513 ...
  $ Col8 : num  0.49 0.172 0.543 0.961 0.314 ...
  $ Col9 : num  0.3511 0.159 0.3041 0.0175 0.9966 ...
  $ Col10: num  0.0676 0.5614 0.0707 0.2114 0.5496 ...
  $ Col12: num  0.715 0.123 0.311 0.946 0.5 ...
  $ Col13: num  0.136 0.785 0.453 0.136 0.885 ...
  $ Col14: num  0.4657 0.0499 0.1874 0.9827 0.3283 ...
  $ Col15: num  0.867 0.732 0.315 0.386 0.332 ...
[1] 0.4070087
On Wed, 25 Dec 2013, Levent TERLEMEZ wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k