Levent TERLEMEZ
2013-Dec-25 17:09 UTC
[R] Erroneous Column Removing Result From User-Defined Function.....
Dear Users,
I have a little problem with user-defined function. I would like to remove
columns from a data frame using a user-defined function but i am getting a
"Error in ad[, -12] : incorrect number of dimensions” error. The
"ad<-ad[,-12]” and similar commands work from R console but i couldn’t
make them run from my function. The function and the type imformation of one of
my obtained data objects is below.
Thanks in advance for your tips and helpful answers,
Levent TERLEMEZ.
Error:
Error in ad[, -12] : incorrect number of dimensions
My Function:
function (dyolu,dadi,dsayi)
{
for(i in 1:dsayi)
{
ad<-paste(dadi,i,sep="")
yol<-paste(dyolu,dadi,i,".csv",sep="")
print(ad)
print(yol)
assign(eval(substitute(ad)),
read.csv(yol,sep=";",dec=",",header=T), envir = globalenv())
ad<-ad[,-12]————————————————————————> Here I would like to remove
some columns but i even can’t remove one. I tried all the suggestion on internet
forums.
#dat[ , -which(names(dat) %in% c("z","u"))]
}
}
dyolu: The string type variable containing the path of the csv files that will
be read.
dadi: The string type variable containing the name of csv files that will be
read.
dsayi: The numeri type variabe containing the number of files that will be read.
My Data Object’s Attributes:> str(be1)
'data.frame': 74 obs. of 16 variables:
$ Avi.TimeStamp: Factor w/ 74 levels " 0:00:40.00",..: 2 1 3 4 5 6 7
8 9 10 ...
$ Frame : int 32949 32950 32952 32953 32954 32955 32957 32961 32962
32963 ...
$ Spot.x : num 274 275 275 275 275 ...
$ Spot.y : num 277 277 277 277 277 ...
$ Pupil.x : num 240 242 242 241 241 ...
$ Pupil.y : num 320 320 320 320 320 ...
$ Pupil.r : num 47.7 47.5 47.6 47.8 47.7 ...
$ Scene.x : num 46.2 30.3 34.6 30.4 43.1 ...
$ Scene.y : num 102.5 71 71.9 73.7 92.2 ...
$ Mouse.x : int 287 287 287 287 287 287 287 287 287 287 ...
$ Mouse.y : int 570 570 570 570 570 570 570 570 570 570 ...
$ X : logi NA NA NA NA NA NA ...
$ H : int 0 0 0 0 0 0 0 0 0 0 ...
$ M : int 0 0 0 0 0 0 0 0 0 0 ...
$ S : int 40 40 41 41 41 41 41 41 41 41 ...
$ Split : int 96 0 8 12 16 20 28 44 48 52 …
This data frame is obtained with the assign command from my function.
[[alternative HTML version deleted]]
Jeff Newmiller
2013-Dec-25 19:15 UTC
[R] Erroneous Column Removing Result From User-Defined Function.....
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 )
> str( dtaList, 1 )
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:> str( dtaList[[ 2 ]] )
'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 ...> mean( dtaList[[ "Dtaprefix2" ]]$Col2 )
[1] 0.4070087
On Wed, 25 Dec 2013, Levent TERLEMEZ wrote:
> Dear Users,
> I have a little problem with user-defined function. I would like to remove
columns from a data frame using a user-defined function but i am getting a
"Error in ad[, -12] : incorrect number of dimensions? error. The
"ad<-ad[,-12]? and similar commands work from R console but i couldn?t
make them run from my function. The function and the type imformation of one of
my obtained data objects is below.
>
> Thanks in advance for your tips and helpful answers,
> Levent TERLEMEZ.
>
>
> Error:
> Error in ad[, -12] : incorrect number of dimensions
>
> My Function:
> function (dyolu,dadi,dsayi)
> {
> for(i in 1:dsayi)
> {
> ad<-paste(dadi,i,sep="")
> yol<-paste(dyolu,dadi,i,".csv",sep="")
> print(ad)
> print(yol)
> assign(eval(substitute(ad)),
read.csv(yol,sep=";",dec=",",header=T), envir = globalenv())
> ad<-ad[,-12]????????????????????????> Here I would like to
remove some columns but i even can?t remove one. I tried all the suggestion on
internet forums.
> #dat[ , -which(names(dat) %in% c("z","u"))]
> }
> }
>
> dyolu: The string type variable containing the path of the csv files that
will be read.
> dadi: The string type variable containing the name of csv files that will
be read.
> dsayi: The numeri type variabe containing the number of files that will be
read.
>
> My Data Object?s Attributes:
>> str(be1)
> 'data.frame': 74 obs. of 16 variables:
> $ Avi.TimeStamp: Factor w/ 74 levels " 0:00:40.00",..: 2 1 3 4 5
6 7 8 9 10 ...
> $ Frame : int 32949 32950 32952 32953 32954 32955 32957 32961 32962
32963 ...
> $ Spot.x : num 274 275 275 275 275 ...
> $ Spot.y : num 277 277 277 277 277 ...
> $ Pupil.x : num 240 242 242 241 241 ...
> $ Pupil.y : num 320 320 320 320 320 ...
> $ Pupil.r : num 47.7 47.5 47.6 47.8 47.7 ...
> $ Scene.x : num 46.2 30.3 34.6 30.4 43.1 ...
> $ Scene.y : num 102.5 71 71.9 73.7 92.2 ...
> $ Mouse.x : int 287 287 287 287 287 287 287 287 287 287 ...
> $ Mouse.y : int 570 570 570 570 570 570 570 570 570 570 ...
> $ X : logi NA NA NA NA NA NA ...
> $ H : int 0 0 0 0 0 0 0 0 0 0 ...
> $ M : int 0 0 0 0 0 0 0 0 0 0 ...
> $ S : int 40 40 41 41 41 41 41 41 41 41 ...
> $ Split : int 96 0 8 12 16 20 28 44 48 52 ?
>
> This data frame is obtained with the assign command from my function.
>
>
> [[alternative HTML version deleted]]
>
>
---------------------------------------------------------------------------
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
Adams, Jean
2013-Dec-26 13:22 UTC
[R] Erroneous Column Removing Result From User-Defined Function.....
Levent,
I would suggest that you get your data the way you want it first, then use
the assign() function at the very end of your for() loop. I also think you
could simplify the arguments in your assign() function.
myfun <- function(dyolu, dadi, dsayi) {
for(i in 1:dsayi) {
ad <- paste(dadi, i, sep="")
yol <- paste(dyolu, dadi, i, ".csv", sep="")
dat <- read.csv(yol, sep=";", dec=",", header=T)
dat <- dat[, -12]
assign(ad, dat, envir=globalenv())
}
}
Jean
On Wed, Dec 25, 2013 at 11:09 AM, Levent TERLEMEZ
<lterlemez@anadolu.edu.tr>wrote:
> Dear Users,
> I have a little problem with user-defined function. I would like to remove
> columns from a data frame using a user-defined function but i am getting a
> "Error in ad[, -12] : incorrect number of dimensions” error. The
> "ad<-ad[,-12]” and similar commands work from R console but i
couldn’t make
> them run from my function. The function and the type imformation of one of
> my obtained data objects is below.
>
> Thanks in advance for your tips and helpful answers,
> Levent TERLEMEZ.
>
>
> Error:
> Error in ad[, -12] : incorrect number of dimensions
>
> My Function:
> function (dyolu,dadi,dsayi)
> {
> for(i in 1:dsayi)
> {
> ad<-paste(dadi,i,sep="")
> yol<-paste(dyolu,dadi,i,".csv",sep="")
> print(ad)
> print(yol)
> assign(eval(substitute(ad)),
> read.csv(yol,sep=";",dec=",",header=T), envir =
globalenv())
> ad<-ad[,-12]————————————————————————> Here I would like to
remove
> some columns but i even can’t remove one. I tried all the suggestion on
> internet forums.
> #dat[ , -which(names(dat) %in% c("z","u"))]
> }
> }
>
> dyolu: The string type variable containing the path of the csv files that
> will be read.
> dadi: The string type variable containing the name of csv files that will
> be read.
> dsayi: The numeri type variabe containing the number of files that will be
> read.
>
> My Data Object’s Attributes:
> > str(be1)
> 'data.frame': 74 obs. of 16 variables:
> $ Avi.TimeStamp: Factor w/ 74 levels " 0:00:40.00",..: 2 1 3 4 5
6 7 8 9
> 10 ...
> $ Frame : int 32949 32950 32952 32953 32954 32955 32957 32961
> 32962 32963 ...
> $ Spot.x : num 274 275 275 275 275 ...
> $ Spot.y : num 277 277 277 277 277 ...
> $ Pupil.x : num 240 242 242 241 241 ...
> $ Pupil.y : num 320 320 320 320 320 ...
> $ Pupil.r : num 47.7 47.5 47.6 47.8 47.7 ...
> $ Scene.x : num 46.2 30.3 34.6 30.4 43.1 ...
> $ Scene.y : num 102.5 71 71.9 73.7 92.2 ...
> $ Mouse.x : int 287 287 287 287 287 287 287 287 287 287 ...
> $ Mouse.y : int 570 570 570 570 570 570 570 570 570 570 ...
> $ X : logi NA NA NA NA NA NA ...
> $ H : int 0 0 0 0 0 0 0 0 0 0 ...
> $ M : int 0 0 0 0 0 0 0 0 0 0 ...
> $ S : int 40 40 41 41 41 41 41 41 41 41 ...
> $ Split : int 96 0 8 12 16 20 28 44 48 52 …
>
> This data frame is obtained with the assign command from my function.
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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]]