0

I am trying to repeat plotting in R my main command is

data<-read.csv(file.choose(),header=TRUE)
t=data[,1]
PCI=data[,2]
plot(t,PCI,xlim=c(0,30))
boxplot(t,PCI,xlim=c(0,30))
# some starting values
alpha=1
betha=1
theta=1
# do the fit
fit = nls(ydata ~ alpha-beta*exp((-theta*(xdata^gama))),      start=list(alpha=80,beta=15,theta=15,gama=-2))

ydata=data[,2]
xdata=data[,1]
new = data.frame(xdata = seq(min(xdata),max(xdata),len=200))
lines(new$xdata,predict(fit,newdata=new))
resid(fit)
qqnorm(resid(fit))

fitted(fit)
resid(fit)
xdata=fitted(fit) 
ydata=resid(fit)
plot(xdata,ydata,xlab="fitted values",ylab="Residuals")
abline(0, 0)

My first column is number of section, my second column is t=x and my third column is PCI=y. I want to repeat my plotting command for each section individually.but I think I can not use loop since the number of data in each section is not equal. I would really appreciate your help since i am new in R.

SecNum  t   PCI
961 1   94.84
961 2   93.04
961 3   91.69
961 11  80.47
961 12  79.26
961 13  77
962 1   90.46
962 2   90.01
962 3   86.88
962 4   86.36
962 5   84.56
962 6   85.11
963 1   91.33
963 2   90.7
963 3   86.46
963 4   88.47
963 5   81.07
963 6   84.07
963 7   82.55
963 8   73.58
963 9   71.85
963 10  83.8
963 11  82.16
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
marmar
  • 21
  • 1
  • 2
    It's easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and desired output. It's unclear what your data looks like or exactly what you've attempted so far to try to solve this problem. – MrFlick Jul 22 '15 at 21:58
  • 2
    Please put periods at the end of complete sentences and please provide some sample data and describe more fully what is needed. Saying "can not use loop since the number of data in each section is not equal" does not really explain the difficulty, since the loop index is not described. The various plotting functions like `points` and `lines` do not need equal numbers of values (although obviously the number of x and y values would be expected to be equal for scatter plots. – IRTFM Jul 22 '15 at 22:00
  • @marmar: Can you maybe accept the answer (if it was helpful) to mark the question as resolved? – MERose Sep 29 '15 at 21:26

1 Answers1

0

To repeat your code for each different SecNum in your data, do something like:

sections <- unique(data$SecNum)

for (sec in sections) {
    # just get the data for that section
    data.section <- subset(data, SecNum == sec)

    # now do all your plotting commands. `data.section` is the
    #  subset of `data` that corresponds to this SecNum only.
}
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194