现在的位置: 首页 > 综合 > 正文

An Brief Visit to R

2013年08月30日 ⁄ 综合 ⁄ 共 1927字 ⁄ 字号 评论关闭

1. Two concepts in R: objects and functions

     Object: a storage space with an associated name;

     Function: a special type of R objects designed to carry out some operation.

2. 赋值:<- (e.g: x<-45);

    列出现有所有对象: ls() or objects()

3. vector: 1)mode: the kind of data stored; 2)length: the number of elements

    使用c()创建length()>1的向量。(e.g. v<-c(1,2,3,4))

    获取向量v的长度:length(v); 获取向量v的mode: node(v).

    某向量中只能包含同一mode的元素。

    NA: special value,代表缺失值。

    []获取向量某元素。(e.g. v[2])

    Vectorization on a vector leads the called function to produce a vector of the same length, with each element resulting from applying the function to the respective element of the original vector

   Recycling: repeating the shorter vector until it fills in the size of the larger vector if longer object length is a multiple of shorter object length. (e.g. v1<-c(4,6,8.24), v2<-(10,2), v1+v2 = 14 8 18 26)

   Factor: ......

4. List: an ordered collection of other objects (a.k.a. components) 类似于结构体。

    components may be of different mode, or length, but are always numbered and may also have a name attached to them. (  the names of the components of a list are, in effect, an attribute of the list)

    unlist(): can unflatten all data in a list and will create a verctor with as many elements as there are data objects in a list.

5. data frame: 一类特殊的list。类似于两维矩阵,不过每一列可存储不同类型的元素。

    使用R内置的电子表格式的界面编辑或新建data frame:

                    1)aDataSet <- edit(aDataSet): 编辑一个存在的data frame aDataSet

                    2)newDataSet <- eidt(data.frame()): 新建一个data  frame newDataSet

6.函数,是一类特殊的对象。一个函数,存储的“值”是一集指令:这些指令,在该函数被调用时,将被执行。新建一个函数,就是使用赋值符 (<-)将函数的内容存储于函数名。

  【 function: as an object, a function can store a value. the value stored in a function is the set of instructions that will be executed  at a call of this function.】

   function.name <- function(<形式参数>)  {<一集指令>}

 

7. objects, classes and methods 对象、类与方法

   everything in R is an object, and every R object belongs to a class.

   The class of an object determines 1)可供使用的genenral方法;2)该类对象的表示。

   note: general的说法,相对于继承该类的其它子类中,可能会有更specific的方法 (polymorphism 多态)

   类的表示:the representation of a class consists of a set of slots. each slot has a name and an associated class that determines the informatuon that it stores

抱歉!评论已关闭.