[R] if else and loop for code in R

Minh Duy Mai minh at cs.au.dk
Fri Sep 25 13:53:03 CEST 2009

I am using if else and loop to sortout the data set that is the values  
less than o or more than 100 will be chosen.I could not get outTable  
with loop.
Please help me to correct the code:

# Read
a_data <- read.table("D:/SNP/copy.sas", header=T, sep="\t")
tr <- a_data$truck
ca <- a_data$cars
length <- nrow(a_data)
outTable <- matrix(nrow=length,ncol=3)

stat <- for (i in 1:length) {
if (tr<0) {0} else
if (ca>100) {0}else
outTable <- c(i, stat, tr)
# Writing the output file
   colnames(outTable) <- c("number", "stat", "tr")
# Graph
plot(stat, type="o", col="red", axes=FALSE, ann=FALSE)
# Create a title with a red, bold/italic font title(main="Autos",  
col.main="red", font.main=4)
# Start PNG device driver to save output to figure.png
png(filename="D:/SNP/figure.png", height=295, width=300, bg="white")
Error: object 'stat' not found
In addition: Warning message:
In if (tr < 0) { :
   the condition has length > 1 and only the first element will be used

Thank alot

More information about the R-help mailing list