Hi guys, I am applying linear regression model using lm, generate the residual and then need to output the result to a csv file. Here is my script: maxtempstation1.lm = lm( Year ~ station1, data=opencsv) maxstation1.res = resid(maxstation1.lm); maxstation1.res write.csv(maxtempstation1.res, file="maxtempstation1.csv") I can do that without problem, but I have 300 stations!. The general idea is to get all the residuals(for all years) into one csv file. Here is a sample of my data. Year ??st1 st2 st3 st4 st5 st6 st7 st8 1982 27.75 27.84 29.81 28.24 29.08 27.97 28.92 28.2 1983 28.74 28.71 29.32 29.34 29.48 28.99 29.45 29.43 1984 28.13 28.34 29.64 28.03 28.85 28.03 28.61 28.08 Anybody willing to help? Thanks in advance guys. Eddie
Hello, Try station <- colnames(opencsv)[-1] mat <- matrix(0, nrow=length(station), ncol=nrow(opencsv)) dimnames(mat) <- list(station, opencsv$Year) for(st in station){ model <- lm(Year~opencsv[, st], data=opencsv) mat[st, ] <- residuals(model) } write.csv(mat, "opencsv.csv") Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Output-result-to-a-csv-file-tp4623510p4623714.html Sent from the R help mailing list archive at Nabble.com.