This is the recommnded way to move R data from one machine to another. Of course you could save it as, say, Excel on one machine and read it in on the other (see below for how). The R dump file, guarantees the abillity to move data between PC's and Mac or Linux machines.
thing <- read.table ("clipboard", sep="\t", header=T)
to create a data.frame named "thing." Notice the sep="\t" indicating that the separator is the tab character when going to or from Excel.
read.table() calls a function named count.fields() to determine the number of fields in each row. The most common cause of a read.table() failure, in my experience, is unequal numbers of fields. Sometimes Excel will add "mystery cells" onto the ends of rows, seemingly just for spite. I don't really know what to do about this -- but see scan() below for one inelegant solution. One more note: the output of read.table() is always a data.frame. If you want a matrix you'll have to turn it into a matrix yourself with as.matrix(). Also, by default character columns are converted into factors. If you want them to stay as characters, use as.is=T.
clipp <- function (x, rn=FALSE) write.table (x, "clipboard-128", sep="\t", row.names=rn, col.names = ifelse (rn, TRUE, NA))Then to move a data.frame to Excel, just clipp() it and paste it: clipp (mydf), alt-tab to move to Excel, and control-V to paste. The rn argument specifies whether you want row names -- if you do, you probably want to set col.names = NA.
# # Count.fields knows the sep= argument # fld.cnt <- count.fields (in.file) # get number of fields # # Set up output matrix, with number of columns given by the largest # value returned by count.fields. Then read in the file. # out.mat <- matrix ("", length(fld.cnt), max (fld.cnt) big.vec <- scan (in.file, what = "") start <- 1 for (i in 1:length(fld.cnt)) { out.mat[i,] <- big.vec[start:(start + fld.cnt[i] - 1)] start <- start + fld.cnt[i] }Notes on scan: Scan() takes a number of arguments, some quite useful. Also scan() (but not count.fields()) will read the Windows clipboard if you specify that the file name is "clipboard." write() is the counterpart of scan() but it's not needed much.
y <- x + 4assign to y the value (x+4). The lines
y <- x + 4assign to y the value x, then execute the legal line "+4", which doesn't have any effect.) An alternative to source()-ing a file full of commands is to use a script file. Actually they're almost exactly the same thing; the difference is that you run a script file with the F10 button, whereas you run the commands in a regular text file by using source().
cat ("Are you sure? (enter y or n) ") response <- readline () if (response == "y" || response == "Y") { ...will display Are you sure? (enter y or n) on the terminal and then wait for the user to respond (that is, type something and hit ENTER). If nothing is entered, "response" is an empty string; otherwise, any characters that the user typed get put into "response."