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

rprog1

Embed Size (px)

Citation preview

 

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 SS: John Chambers bell labsS in 1976 Fortran libraries

R: Ross Ihaka and Robert Gentleman

#: commentevaluation : auto printing and expliciting1 indicates x is vector 5 first element

atomic classes of objects:characternumericintegercomplexlogical(True/False)

most basic object: vectorvector: objects of same classlist: vector contains objects of diff classesempty vectors can be cretaed by vector() fnvactor first argument class second lengthnumbers: double precision numeric1: numeric object 1L integerInf: infinityNaN: undefined value can be thought as missing value

Attribute:names,dinamesdimensions(matrices,array)classlengthother user defined attributes/metadatAttributes 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 vectoris of same classexplicit 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 themm <- matrix(nrow=2,ncol=3)mdim(m) : no of rows columnsattributes(m)

attribute column wise: upper left corner and running down colsvector 2,5 assigned to dim attribute

matrix created by column and row bindingx <- 1:3y <-10,12cbind(x,y)rbind(x,y)

factors unordered and orderedfactor: integer vector integer has labelmodeling fns : lm() and gm()

factor input: character vectortable()unclass(): integer vectorattr(,"levels")

levels=c("yes","no")

NA NaNis.NA:is.NaNNA cass integer characterNaN is NA but converse not true

data frames: list(each element of list column and lenght of each element rows)data frame row namerow.namesdataframes 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 namesx <- 1:3names(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 datareadLines : reading lines of text filesource: for reading in R code files (inverse of dump)dget for reading in R code files(inverse of dput)load for reading in saved workspacesunserialisable for reading single R objects in binary form

write fns:

 

write.tablewriteLinesdumpdputsaveserialize

read.table args1. file

2. header logical if file has header line3. sep: string indicating how cols are separated4. colClasses: character vector indicating class of each column in dataset5. nrows: number of rows in dataset6. comment.char: character string indicating comment character7. skip: number of lines to skip from beginnning8. stringsAsFactors: should character variables be coded as factors

read.csv: default separator comma  header always equals true

help page for read.tableif dataset> amount of ram on computer stop herecomment.char= " " if there are no commented lines in your fileclasses <- sapply(initial,class)

textual formats:dumping and dputingit 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 filegzfile 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 indexDROP drops dimensionEXACT FALSE[[]] DOESNT DO PARTIAL MATCHING

 

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