[R] help with apply (lapply or sapply not sure)

PIKAL Petr petr.pikal at precheza.cz
Fri Jul 26 08:53:15 CEST 2013


Hi

I would just use for cycle. Something like

lll <- vector("list", length(InputFiles))
for (i in InputFiles) {

everything what you want to do for each file
you can store it in a list
lll[[i]] <- whatever you want to store there

}

After populating list you can do anything with it.

Regards
Petr

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Robert Lynch
> Sent: Friday, July 26, 2013 2:46 AM
> To: r-help at r-project.org
> Subject: [R] help with apply (lapply or sapply not sure)
> 
> I am reading in a bunch of files and then processing them all in the
> same
> way.
> 
> I am sure there as a better way then to copy and past the code for each
> file. Here is what I've done so far
> 
> InputFiles<-
> as.character(list.files("~/ISLE/RWork/DataWarehouseMining/byCourse/"))
> #Path to the Course data files
> 
> for (i in InputFiles) {
>   #
> print(head(read.csv(paste("~/ISLE/RWork/DataWarehouseMining/byCourse/",
> i, sep=""))))
>   print(paste("Reading file
> ~/ISLE/RWork/DataWarehouseMining/byCourse/", i,
> sep=","))
>   assign(i,
> read.csv(paste("~/ISLE/RWork/DataWarehouseMining/byCourse/", i,
> sep="")))
> }#note last file is NOT a course file by the student information.
> Master<-StudentInfoForRobertWUnitAt7A_2.csv #this is the last file
> 
> 
> CourseFiles <-InputFiles[- c(15,16)] # ignore the student info
> ...7A.csv &
> ...7A_2.csv
> 
> #for each file I do the following
> #Bis 101
> summary(BigInstBIS101.csv)
> B101 <- BigInstBIS101.csv[-c(3,4,8)]
> summary(B101)
> B101$WH_ID <- as.factor(B101$WH_ID)
> B101$SID <- as.factor(B101$SID)
> B101$TERM <- as.factor(B101$TERM)
> B101$CRN <- as.factor(B101$CRN)
> B101$CRN_TRM <- as.factor(B101$CRN_TRM)
> B101$INST_NUM <- as.factor(B101$INST_NUM)
> B101$zGrade <- with(B101, ave(GRADE., list(TERM, INST_NUM), FUN =
> scale))
> write.csv(B101,"B101.csv", row.names = FALSE)
> 
> #Bis 2A
> B2A <- BigInstBIS2A.csv[-c(3,4,8)]
> summary(B2A)
> B2A$WH_ID <- as.factor(B2A$WH_ID)
> B2A$SID <- as.factor(B2A$SID)
> B2A$TERM <- as.factor(B2A$TERM)
> B2A$CRN <- as.factor(B2A$CRN)
> B2A$CRN_TRM <- as.factor(B2A$CRN_TRM)
> B2A$INST_NUM <- as.factor(B2A$INST_NUM)
> B2A$zGrade <- with(B2A, ave(GRADE., list(TERM, INST_NUM), FUN = scale))
> write.csv(B2A,"B2A.csv", row.names = FALSE)
> 
> 
> And so on for another 12 courses, however I am changing what I am doing
> as
> part of the reading in the file and don't want to replace the code in
> 14
> different places.
> suggestions?  Thanks!
> 
> 	[[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