全部版块 我的主页
论坛 新商科论坛 四区(原工商管理论坛) 商学院 案例库
3398 0
2013-03-18
资料来源:网络
注:部分英文已在小括号中给出翻译,限于水平,翻译部分仅供参考。

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.


Vectors(向量)a <- c(1,2,5.3,6,-2,4) # numeric vector(数值型向量)
b <- c("one","two","three") # character vector(字符型向量)
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector(逻辑型向量)

Refer to elements of a vector using subscripts.
a[c(2,4)] # 2nd and 4th elements of vector

Matrices(矩阵)All columns in a matrix must have the same mode(numeric, character, etc.) and the same length. The general format is
mymatrix <- matrix(vector, nrow=r, ncol=c, byrow=FALSE,
   dimnames=list(
char_vector_rownames, char_vector_colnames))
byrow=TRUE indicates that the matrix should be filled by rows. byrow=FALSE indicates that the matrix should be filled by columns (the default). dimnames provides optional labels for the columns and rows.
# generates 5 x 4 numeric matrix (生成一个5 * 4 数值型矩阵)
y<-matrix(1:20, nrow=5,ncol=4)

# another example
cells <- c(1,26,24,68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
  dimnames=list(rnames, cnames))

Identify rows, columns or elements using subscripts.
x[,4] # 4th column of matrix
x[3,] # 3rd row of matrix
x[2:4,1:3] # rows 2,3,4 of columns 1,2,3


Arrays(数组)Arrays are similar to matrices but can have more than two dimensions. See help(array) for details.
(数组与矩阵基本相同,但是可以超过两个维度。具体见help(array).)

Data Frames(数据框)A data frame is more general than a matrix, in that different columns can have different modes (numeric, character, factor, etc.). This is similar to SAS and SPSS datasets.(数据框比矩阵更加普遍,可以存在不同形式的变量,如数值型、字符型等,这与SAS和SPSS中的数据集相似。)
d <- c(1,2,3,4)
e <- c("red", "white", "red", NA)
f <- c(TRUE,TRUE,TRUE,FALSE)
mydata <- data.frame(d,e,f)
names(mydata) <- c("ID","Color","Passed") # variable names

There are a variety of ways to identify the elements of a data frame .
myframe[3:5] # columns 3,4,5 of data frame
myframe[c("ID","Age")] # columns ID and Age from data frame
myframe$X1 # variable x1 in the data frame


Lists(列表)An ordered collection of objects (components). A list allows you to gather a variety of (possibly unrelated) objects under one name.
# example of a list with 4 components - (包含四个部分的一个列表举例如下)
# a string, a numeric vector, a matrix, and a scaler (一个字符串,一个数值型向量,一个矩阵和一个测量维度)
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3)

# example of a list containing two lists
v <- c(list1,list2)

Identify elements of a list using the [[]] convention.
mylist[[2]] # 2nd component of the list
mylist[["mynumbers"]] # component named mynumbers in list


Factors(因子)Tell R that a variable is nominal by making it a factor. The factor stores the nominal values as a vector of integers in the range [ 1... k ] (where k is the number of unique values in the nominal variable), and an internal vector of character strings (the original values) mapped to these integers.

# variable gender with 20 "male" entries and
# 30 "female" entries (性别变量中有20个男性条目和30个女性条目)
gender <- c(rep("male",20), rep("female", 30))
gender <- factor(gender)
# stores gender as 20 1s and 30 2s and associates(R内部会根据性别变量的首字母顺序用1代表女性,2代表男性共存储20个“1”和30个“2”)
# 1=female, 2=male internally (alphabetically)
# R now treats gender as a nominal variable
summary(gender)


An ordered factor is used to represent an ordinal variable. (一个顺序因子被用于代表一个有序变量)
# variable rating coded as "large", "medium", "small'(“大”、“中”“小”三个变量)
rating <- ordered(rating)
# recodes rating to 1,2,3 and associates(以1,2,3来记录)
# 1=large, 2=medium, 3=small internally
# R now treats rating as ordinal


R will treat factors as nominal variables and ordered factors as ordinal variables in statistical proceedures and graphical analyses. You can use options in the factor( ) and ordered( ) functions to control the mapping of integers to strings (overiding the alphabetical ordering). You can also use factors to create value labels.

Useful Functions
length(object) # number of elements or components(项目中包含元素或组成部分的个数)
str(object)    # structure of an object (项目的结构)
class(object)  # class or type of an object(项目的类别)
names(object)  # names(项目名称)

c(object,object,...)       # combine objects into a vector(将项目包含于一个向量中)
cbind(object, object, ...) # combine objects as columns(以列的形式合成项目)
rbind(object, object, ...) # combine objects as rows (以行的形式合成项目)

object     # prints the object(输出项目)

ls()       # list current objects(列出当前项目)
rm(object) # delete an object(删除项目)

newobject <- edit(object) # edit copy and save as newobject (以新项目的形式编辑复制和保存)
fix(object)               # edit in place (在当前出口修改)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群