Вы находитесь на странице: 1из 4

getwd()

read.csv("mydata.csv")
function:
myfunction <- function(){
x <- rnorm(100)
mean(x)
myfunction <- function(){
x <- rnorm(100)
mean(x)
}
second <- function(x){
x+rnorm(length(x))
}
R is a dialect of S
S: John Chambers bell labs
S in 1976 Fortran libraries
R: Ross Ihaka and Robert Gentleman
#: comment
evaluation : auto printing and expliciting
1 indicates x is vector 5 first element
atomic classes of objects:
character
numeric
integer
complex
logical(True/False)
most basic object: vector
vector: objects of same class
list: vector contains objects of diff classes
empty vectors can be cretaed by vector() fn
vactor first argument class second length
numbers: double precision numeric
1: numeric object 1L integer
Inf: infinity
NaN: undefined value can be thought as missing value
Attribute:
names,dinames
dimensions(matrices,array)
class
length
other user defined attributes/metadat
Attributes of object accessed by attributes() fn
c(): create vector of objects c(0.5,0.6)
x <- vector("complex",length=10)
different objects in vector coercion occurs so that every element in the vector
is of same class
explicit coercion:
use as

as.numeric(x)
class(x)
NAs for unsensible coercion
x <- list(1,"a",TRUE)
elements of list will have double brackets around them
m <- matrix(nrow=2,ncol=3)
m
dim(m) : no of rows columns
attributes(m)
attribute column wise: upper left corner and running down cols
vector 2,5 assigned to dim attribute
matrix created by column and row binding
x <- 1:3
y <-10,12
cbind(x,y)
rbind(x,y)
factors unordered and ordered
factor: integer vector integer has label
modeling fns : lm() and gm()
factor input: character vector
table()
unclass(): integer vector
attr(,"levels")
levels=c("yes","no")
NA NaN
is.NA:
is.NaN
NA cass integer character
NaN is NA but converse not true
data frames: list(each element of list column and lenght of each element rows)
data frame row namerow.names
dataframes called by read.table() read.csv()
converted to matrix data.matrix()
x <- data.frame(foo = 1:4,bar=c(T,T,F,F))
nrow(x) nrow(y)
R objects can have names
x <- 1:3
names(x)
names(x) <- c("foo","bar","kl")
list(a=1,b=2,c=3)
dimnames(m) <- list(c("a","b"),c("c","d"))
read.csv read.table tabular data
readLines : reading lines of text file
source: for reading in R code files (inverse of dump)
dget for reading in R code files(inverse of dput)
load for reading in saved workspaces
unserialisable for reading single R objects in binary form
write fns:

write.table
writeLines
dump
dput
save
serialize
read.table args
1. file
2.
3.
4.
5.
6.
7.
8.

header logical if file has header line


sep: string indicating how cols are separated
colClasses: character vector indicating class of each column in dataset
nrows: number of rows in dataset
comment.char: character string indicating comment character
skip: number of lines to skip from beginnning
stringsAsFactors: should character variables be coded as factors

read.csv: default separator comma


header always equals true
help page for read.table
if dataset> amount of ram on computer stop here
comment.char= " " if there are no commented lines in your file
classes <- sapply(initial,class)
textual formats:dumping and dputing
it contains metadata(classes,different cols)
work well with version control system
y <- data.frame(a=1,b="a")
dput(y)
dump(c("x","y"),file="data.R")
rm(x,y)
source("data.R")
y
file opens connection to file
gzfile bzfile url
con <- file("foo.txt","r")
data <-read.csv(con)
close(con)
con <-gzfile("words.gz")
x <-readLines(con,10)
con <-url("http://www.mmv.nvm","r")
x <-readLines(con)
head(x)
[: same object of class
[[: extract elements of list or data frame
$ : extract elements of list or data frame by name ; semantics similar to hat of
[[
logical and numeric index
DROP drops dimension
EXACT FALSE
[[]] DOESNT DO PARTIAL MATCHING

air[1:6,]
good <-complete.cases(air)
air[g,][1:6,]
x%*%y (matrix multiplication)