R语言的数据类型较多,但都是动态声明,即变量不会声明为某种数据类型。变量分配为R对象数组
# Atomic vector of type character. print("abc");#character # Atomic vector of type double. print(12.5)#numeric # Atomic vector of type integer. print(63L)#integer # Atomic vector of type logical. print(TRUE)#logical # Atomic vector of type complex. print(2+3i)#complex # Atomic vector of type raw. print(charToRaw('hello'))#raw
最简单的是向量类型,即便用c()
的形式声明。
如下示例中,若是其中一个元素是字符,则非字符值被强制转换为字符类型app
# The logical and numeric values are converted to characters. s <- c('apple','red',5,TRUE) print(s)
实际上,向量的多元素能够用冒号表示,好比函数
v <- 6.6:12.6 print(v) w <- 3.8:11.4
即表示从6.6到12.6,逐次加一构成的向量;w表示从3.8逐次加一到10.8。还能够用函数建立:编码
# Create vector with elements from 5 to 9 incrementing by 0.4. print(seq(5, 9, by = 0.4))
若是其中一个元素是字符,则非字符值被强制转换为字符类型。spa
# The logical and numeric values are converted to characters. s <- c('apple','red',5,TRUE) print(s)
访问向量元素时,能够用'[ ]'填入适当条件做为索引。而且,向量支持数值运算,但必须是相同大小的。code
建立列表用list函数,而且其中能够包含几乎任何数据类型,能够给list中的每一个元素命名。对象
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Show the list. print(list_data)
访问列表元素既能够用序号直接索引,也能够用名称索引索引
# Access the first element of the list. print(list_data[1]) # Access the list element using the name of the element. print(list_data$A_Matrix)
操纵列表元素时,直接进行赋值操做。另外能够经过merged.list <- c(list1,list2)
合并列表。ip
# Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2)
在R语言中建立矩阵的基本语法是ci
matrix(data, nrow, ncol, byrow, dimnames)
dimname是分配给行和列的名称。
访问矩阵的元素直接用中括号填入矩阵下标访问,即\(a_{23}=M[2,3]\)。或者用单一下标直接访问整行或整列,即\(a_{13},a_{23},\cdots,a_{m3}=M[,3]\)。
使用R运算符对矩阵执行各类数学运算。 操做的结果也是一个矩阵。对于操做中涉及的矩阵,维度(行数和列数)应该相同。
# Create two vectors of different lengths. vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) column.names <- c("COL1","COL2","COL3") row.names <- c("ROW1","ROW2","ROW3") matrix.names <- c("Matrix1","Matrix2") # Take these vectors as input to the array. result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names, matrix.names)) print(result)
一样的,数组的访问相似于矩阵,以上数组有三个维度,访问时用中括号以及两个逗号能够提取一个,或多个元素
print(array[1,3,4]) print(array[3, ,2]) print(array[2, , ])
操做数组的元素经过访问数组的部分元素来执行。好比能够用两个逗号和一个维度的数字,来提取出矩阵。
咱们可使用apply()
函数在数组中的元素上进行计算。
apply(x, margin, fun)
fun是要应用于数组元素的函数
从而进行数组内部的运算
f <- factor(x=charactor(), levels, labels=levels, exclude = NA, ordered = is.ordered(x), namax = NA)
gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
factor()
函数能够把向量data转化为factor。简单来讲,因子就是一段具备二元层级顺序的有限序列,print打印出的是其level层级。数据帧(data.frame)中的每一列也可看作因子。v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston")) print(v) # 结果为 Tampa Tampa Tampa Tampa Seattle Seattle Seattle Seattle Boston [10] Boston Boston Boston Levels: Tampa Seattle Boston
建立数据帧
# Create data frame new.address <- data.frame( city = c("Lowry", "Charlotte"), state = c("CO", "FL"), zipcode = c("80230", "33949"), stringsAsFactors = FALSE )
而且经过str()
函数能够看到数据帧的结构。能够经过应用summary()
函数获取数据的统计摘要和性质。也能够提取
# Extract Specific columns. result <- data.frame(emp.data$emp_name,emp.data$salary) print(result) # 先提取前两行,再提取全部列 # Extract first two rows. result <- emp.data[1:2,] # 也能够一并提取 result <- emp.data[c(3,5),c(2,4)]
要扩展数据帧只需使用新的列名称添加列向量,注意要使用$对数据帧名称进行索引。或者,添加行用rbind()
函数,添加列用cbind()
。