Created
August 9, 2013 07:11
-
-
Save aaronsaunders/6191713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("Hmisc",dependencies=TRUE) | |
update.packages() | |
q() | |
setwd() | |
getwd() | |
ls() # lists objects | |
rm(object) # deletes an object | |
library(RColorBrewer) # load ColorBrewer palettes | |
library(vegan) # load vegan package | |
head(mydata) | |
tail(mydata) | |
########## | |
# Entering data | |
c = scan() # prompts for input from the use at the cmd | |
# works for columns from excel | |
# values are separated by whitespace | |
# can be cut and pasted from excel | |
read.table(file, header = FALSE, sep = "", quote = "\"'", | |
dec = ".", row.names, col.names, | |
as.is = !stringsAsFactors, | |
na.strings = "NA", colClasses = NA, nrows = -1, | |
skip = 0, check.names = TRUE, fill = !blank.lines.skip, | |
strip.white = FALSE, blank.lines.skip = TRUE, | |
comment.char = "#", | |
allowEscapes = FALSE, flush = FALSE, | |
stringsAsFactors = default.stringsAsFactors(), | |
fileEncoding = "", encoding = "unknown", text) | |
# tab-delimited | |
read.delim(file, header = TRUE, sep = "\t", quote="\"", dec=".", | |
fill = TRUE, comment.char="", ...) | |
read.delim2(file, header = TRUE, sep = "\t", quote="\"", dec=",", | |
fill = TRUE, comment.char="", ...) | |
# comma-delimited | |
read.csv(file, header = TRUE, sep = ",", quote="\"", dec=".", | |
fill = TRUE, comment.char="", ...) | |
read.csv2(file, header = TRUE, sep = ";", quote="\"", dec=",", | |
fill = TRUE, comment.char="", ...) | |
data1 = read.table('c:/aaron/R/file1.txt') | |
data1 = read.table(file.choose()) # opens a file browser | |
data1 = read.csv(file = 'c:/aaron/R/file1.csv', head = TRUE, sep = ";") # reads cvs with ';' delimiter. first row are column names | |
# Opening Excel | |
# Datasets are often saved as Excel spreadsheets. Here we utilize the xlsx package and Java to download an Excel dataset. | |
# these two steps only needed to read excel files from the internet | |
f <- tempfile("hsb2", fileext=".xls") | |
download.file("http://www.ats.ucla.edu/stat/data/hsb2.xls", f, mode="wb") | |
dat.xls <- read.xlsx(f, sheetIndex=1) | |
# If you have trouble getting Java and the xlsx package installed and working, just click "save as" in Excel and export the data to a comma separated values file (.csv). | |
################# | |
# Saving data | |
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", | |
eol = "\n", na = "NA", dec = ".", row.names = TRUE, | |
col.names = TRUE, qmethod = c("escape", "double"), | |
fileEncoding = "") | |
write.csv(...) | |
write.csv2(...) | |
x <- data.frame(a = I("a \" quote"), b = pi) | |
write.table(x, file = "foo.txt", sep = "\t", col.names = TRUE) | |
tempfile(pattern = "file", tmpdir = tempdir(), fileext = "") | |
tempdir() | |
# write.xlsx comes from the xlsx package. | |
write.xlsx(dat.csv, file = "path/to/save/filename.xlsx", sheetName="hsb2") | |
# save to binary R format (can save multiple datasets and R objects) | |
# save(dat.csv, dat.dta, dat.spss, dat.txt, file = "path/to/save/filename.RData") | |
files <- list.files(pattern=".txt$") | |
for(i in files) { | |
x <- read.table(i, header=TRUE, comment.char = "A", sep="\t") | |
assign(i, x) | |
print(i) | |
write.table(x, paste(i, c(".out"), sep=""), quote=FALSE, sep="\t", col.names = NA) | |
} | |
###################### | |
# Binary R obejects | |
save.image() # saves the workspace | |
save(object, file='filename.RData') | |
load('filename.RData') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment