[R] how to edit my R codes into a efficient way
Hello, Everyone, I am a student an a new learner of R and I am trying to do my homework in R. I have 10 files need to be read and process seperately. I really want to write the codes into something like macro to save the lines instead of repeating 10 times of similar work. The following is part of my codes and I only extracted three lines for each repeating section. data.1 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat1.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); data.2 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat3.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); data.3 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat4.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); baby.1 - data.frame(cuff=data.1$avg_value, time=seq(1,dim(data.1)[1]), patient=rep(1, dim(data.1)[1])) baby.2 - data.frame(cuff=data.2$avg_value, time=seq(1,dim(data.2)[1]), patient=rep(3, dim(data.2)[1])) baby.3 - data.frame(cuff=data.3$avg_value, time=seq(1,dim(data.3)[1]), patient=rep(4, dim(data.3)[1])) I also tried the codes below but it doesn't work. for(n in 1:10){ mm - data.frame(cuff=paste(data,n, sep=.)$avg_value, time=seq(1,dim(paste(data,n, sep=.))[1]), patient=rep(1,paste(data,n, sep=.))[1])) assign(paste(baby,n,sep=.), mm)} I am looking forward to your help and thanks very much! Xuhong __ R-help@stat.math.ethz.ch 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.
Re: [R] how to edit my R codes into a efficient way
Again, thanks a lot. Xuhong On 3/6/07, Petr Klasterecky [EMAIL PROTECTED] wrote: Xuhong Zhu napsal(a): Hello, Everyone, I am a student an a new learner of R and I am trying to do my homework in R. I have 10 files need to be read and process seperately. I really want to write the codes into something like macro to save the lines instead of repeating 10 times of similar work. The following is part of my codes and I only extracted three lines for each repeating section. data.1 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat1.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); data.2 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat3.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); data.3 - read.csv(http://pegasus.cc.ucf.edu/~xsu/CLASS/STA6704/pat4.csv;, header = TRUE, sep = ,, quote = , fill = TRUE); baby.1 - data.frame(cuff=data.1$avg_value, time=seq(1,dim(data.1)[1]), patient=rep(1, dim(data.1)[1])) baby.2 - data.frame(cuff=data.2$avg_value, time=seq(1,dim(data.2)[1]), patient=rep(3, dim(data.2)[1])) baby.3 - data.frame(cuff=data.3$avg_value, time=seq(1,dim(data.3)[1]), patient=rep(4, dim(data.3)[1])) I also tried the codes below but it doesn't work. for(n in 1:10){ mm - data.frame(cuff=paste(data,n, sep=.)$avg_value, time=seq(1,dim(paste(data,n, sep=.))[1]), patient=rep(1,paste(data,n, sep=.))[1])) assign(paste(baby,n,sep=.), mm)} This cannot work since paste() gives you quoted character output while functions like data.frame() etc expect a name of some R object. You can use paste when reading individual csv files: for(n in 1:10){ mydata - read.csv(file=paste('...STA6704/pat',n,'.csv',sep=), header = TRUE, sep = ,, quote = , fill = TRUE) # ... further lines to process mydata ... } A faster way of computing would involve reading the individual files into a list of dataframes and using lapply() on that list rather than processing the data inside the loop. Petr Xuhong -- Petr Klasterecky Dept. of Probability and Statistics Charles University in Prague Czech Republic __ R-help@stat.math.ethz.ch 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.
[R] Question about the smooth.Pspline
Hello, Everyone, I want to use the smooth.Pspline to smooth my data but R give me the error message as follows: Error in smooth.Pspline(sort.e$time, sort.e$cuff, method = 3) : X not strictly increasing my data looks like the following: id cuff time patient ... 2783 13.229608 478 6 3472 20.904825 478 7 4155 15.033727 478 8 4845 19.342963 478 9 715 8.00 479 3 1422 22.052385 479 4 2110 15.393063 479 5 2784 13.200922 479 6 3473 20.900132 479 7 ... my R codes is: e - rbind(patient.1,patient.2,patient.3,...) attach(e) sort.e - e[order(time),] plot(sort.e$time, sort.e$cuff, xlab=Time, ylab=Cuff,type=p, col=3, xlim=c(c[2],d[2]) , ylim=c(c[1], d[1]) , main=one Smooth Curve for 10 Patients) fm - smooth.Pspline(sort.e$time, sort.e$cuff, method=3) lines(fm$x, fm$y, lty=1,col=1) What I am doing here is to combine the data together and find a smooth curve. My question is if the smooth.Pspline could not be used in my data since the variable time has repeated values. Again, thanks for your time and kindly help! Xuhong __ R-help@stat.math.ethz.ch 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.