myframe <-edit(data.frame()) myframe ls() rm(myframe) ls() ########################################################## # read.table() # Reads a file in table format and creates a data frame # from it, with cases corresponding to lines and # variables to fields in the file. # File "first.dat" # # var1,var2 # this is , 11.4 # a test., 12.3 first <- read.table("first.dat", header=TRUE, sep=",") first mean(first.var2) attach(first) mean(var2) ########################################################### # scan() # Read data into a vector or list from the console or file # cat("TITLE extra line", "2 3 5 7", "11 13 17", file="ex.data", sep="\n") pp <- scan("ex.data", skip = 1, quiet= TRUE) scan("ex.data", skip = 1) scan("ex.data", skip = 1, nlines=1)# only 1 line after the skipped one scan("ex.data", what = list("","","")) # flush is F -> read "7" scan("ex.data", what = list("","",""), flush = TRUE) unlink("ex.data") # tidy up ############################################################ # sink() # diverts R output to a connection # sink("sink-examp.txt") i <- 1:10 outer(i, i, "*") sink() unlink("sink-examp.txt") ## Not run: ## capture all the output to a file. zz <- file("all.Rout", open="wt") sink(zz) sink(zz, type="message") try(log("a")) ## back to the console sink(type="message") sink() try(log("a")) ## End(Not run) ########################################################### # write.table() and read.table() a <-c(5:8); b <- c(8:11); c<-c("This","is","a","test") x <-data.frame(a,b,c) write.table(x,file="foo.txt") read.table("foo.txt") ########################################################### # write.csv() and read.csv() # write.csv(x,file="foo.csv",row.names=FALSE) read.csv("foo.csv") ########################################################### # a <- rnorm(100) # generate the data x <- 1:100 # create the x-axis z <- lm(a~x) # z becomes a linear model of "a" depending on "x" z # see that z is just an intercept and slope plot(a) # plot(a) abline(z) # plot the best-fit line above. ########################################################### # ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959 print( ts(1:10, freq = 7, start = c(12, 2)), calendar = TRUE) # print.ts(.) ## Using July 1954 as start date: gnp <- ts(cumsum(1 + round(rnorm(100), 2)), start = c(1954, 7), frequency = 12) plot(gnp) # using 'plot.ts' for time-series plot ## Multivariate z <- ts(matrix(rnorm(300), 100, 3), start=c(1961, 1), frequency=12) class(z) plot(z) plot(z, plot.type="single", lty=1:3)