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
<lterle...@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]]

______________________________________________
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.

Reply via email to