[R] Formatting numerical output
MARCEL CURLIN
cemarcel at u.washington.edu
Sun Apr 25 00:14:12 CEST 2010
Hello,
I am new to R and am having difficulty formatting numerical output from a regression analysis. My code iteratively performs linear regression on a dataset while excluding certain data ranges.
My code:
rm(list = ls(all = TRUE))
sink("outfile")
dat <- read.table("testdat", sep="\t", header=TRUE)
int = 0.2
for (x in c(0:20)) {
subdat <- subset(dat, time <= int * x | time > (int*x) + int) #excludes range of time data between int * x and (int*x) + int
lm.subdat <- lm(length~time, subdat) #regression
rs.subdat <- summary(lm.subdat)$r.squared #getting R-squared information
txt1 <- ("Excluded range: Time") #creating components of output message
txt2 <- ("R^2 =") #creating components of output message
lowend <- (int*x)
highend <- (int*x + int)
output <- c(txt1, lowend, highend, txt2, rs.subdat)
print.noquote(output, sep="\t")
}
sink()
Currently my output looks like:
[1] Excluded range: Time 0 0.2
[4] R^2 = 0.111526872884505
[1] Excluded range: Time 0.2 0.4
[4] R^2 = 0.0706332920267015
[1] Excluded range: Time 0.4 0.6
[4] R^2 = 0.0691466100802879
I would like the output format to look like:
Excluded range: Time 1.0 - 1.2<tab>R^2 = 0.45
Excluded range: Time 1.2 - 1.4<tab>R^2 = 0.5
etc.
I would like to
1. get time and R^2 data on the same line
2. control (reduce) the number of digits reported for R^2
3. reduce the large number of empty spaces between "R^2' and value.
I searched a lot but could not find much on this. Any help on these specifics or general comments on formatting numerical output greatly appreciated.
thanks,
Marcel
More information about the R-help
mailing list