[R] error in a abline loop

Pascal Oettli kridox at ymail.com
Tue Jan 8 02:41:44 CET 2013


Hello,

A ")" is missing.

I guess your code should be

for (i in 1:7){
subs <- data$skin_color==levels(data$skin_color)[i]
line <-lm(body_weight~body_length, data=subset(data, subset=subs)) 
abline(line,col=c("yellow","chocolate1","darkorange2","red3","saddlebrown","coral4","grey38")[i],lwd=2)
}

HTH,
Pascal

Le 08/01/2013 10:23, Elaine Kuo a écrit :
> Hello
>
> I have data of body length and body weight of people of different skin colors.
>
> I tried to write a code to plot body length and body weight according
> to the skin colors.
> (Thanks for Petr's advice so far.)
>
> A loop is used but an error shows up in the following code.
> It says:
>   unexpected '}' in
> "    "red3","red3","saddlebrown","coral4","chocolate4","darkblue","navy","grey38")[i],lwd=2)
>      }"
>
> Please kindly advise how to modify the code.
> Thank you.
>
> The code
>    data <-read.csv("H:/skincolor.csv",header=T)
>
>    # graph
>      par(mai=c(1.03,1.03,0.4,0.4))
>
>      plot(data$body_weight, data$body_length,
>      xaxp=c(0,200,4),
>      yaxp=c(0,200,4),
>      type="p",
>      pch=1,lwd=1.0,
>      cex.lab=1.4, cex.axis=1.2,
>      font.axis=2,
>      cex=1.5,
>      las=1,
>      bty="l",col=c("yellow","chocolate1","darkorange2",
> "red3","saddlebrown","coral4","grey38")[as.numeric(data$skin_color)])
>
>
>      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>      ##
>      for (i in 1:7) {
>      subs <- data$skin_color==levels(data$skin_color)[i]
>      line<-lm(body_weight~body_length, data=subset(data, subset=subs),
>      abline(line,col=c("yellow","chocolate1","darkorange2",
> "red3","saddlebrown","coral4","grey38")[i],lwd=2)
>      }
>
> ______________________________________________
> 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