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. ______________________________________________ R-help@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.