1. 程式人生 > >R語言學習(四)——對資料進行操作

R語言學習(四)——對資料進行操作

  • 判斷變數的屬性
is.character(x)       #判斷是否為字元型
is.numeric(x)         #判斷是否為數值型
is.vector(x)          #判斷是否為一個向量
is.matrix(x)          #判斷是否為一個矩陣   
is.array(x)           #判斷是否為一個數組
is.data.frame(x)      #判斷是否為一個數據框
  • 建立一個矩陣
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
> x <- matrix(1:20,nrow=5,ncol=4,byrow=T) 
> x 
[,1] [,2] [,3] [,4] 
[1,] 1 2 3 4 
[2,] 5 6 7 8 
[3,] 9 10 11 12 
[4,] 13 14 15 16 
[5,] 17 18 19 20

> is.matrix(x) 
[1] TRUE

> dim(x) #檢視或設定陣列的維度向量 
[1] 5 4
注意:此時不可以再向dim(x)賦值,如:dim(x) <- c(6,4) ,會有如下報錯:
Error in dim(x) <- c(4, 4) : dims [product 16] 因為與物件長度[20]不匹配
但是,可以這樣使用:
> x <- 1:20 
> dim(x) <- c(5,4)  
> x 
[,1] [,2] [,3] [,4] 
[1,] 1 6 11 16 
[2,] 2 7 12 17 
[3,] 3 8 13 18 
[4,] 4 9 14 19 
[5,] 5 10 15 20

> attributes(x)  #使用attributes()函式將返回一個列表,其中的第一個元素是dim,dim中包含向量(5,4)
$`dim`
[1] 5 4

還可以利用cbind()和rbind()函式來建立矩陣

> x<-1:3
> x
[1] 1 2 3
> y<-10:12
> y
[1] 10 11 12
> cbind(x,y)
     x  y
[1,] 1 10
[2,] 2 11
[3,] 3 12
> rbind(x,y)
  [,1] [,2] [,3]
x    1    2    3
y   10   11   12

提取對角線元素

> diag(x)

將一個矩陣變成上三角矩陣

> x[lower.tri(y)]<-NA

將一個矩陣變成下三角矩陣

> x[upper.tri(y)]<-NA
  • 建立一個列表

列表是可以包含多種型別的物件的向量。

> x<-list(1,"A",FALSE,5+6i)
> x
[[1]]
[1] 1

[[2]]
[1] "A"

[[3]]
[1] FALSE

[[4]]
[1] 5+6i

#列表還可以這樣訪問
> x[1]
[[1]]
[1] 1

> x[2]
[[1]]
[1] "A"

> x[3]
[[1]]
[1] FALSE

> x[4]
[[1]]
[1] 5+6i
  • 建立一個數組
array(data = NA, dim = length(data), dimnames = NULL)
> x<-array(2:6,c(2,4))  #生成一個數值在2到6之間的陣列,這個陣列為兩行四列
> x
     [,1] [,2] [,3] [,4]
[1,]    2    4    6    3
[2,]    3    5    2    4
  • 將矩陣轉換為資料框
as.data.frame(x)
  • 檢視或設定行名
rownames(x) 
rownames(x) <- c(‘a’,’b’,’c’,’d’,’e’) 
  • 檢視或設定列名
colnames(x) 
colnames(x) <- c(‘a’,’b’,’c’,’d’,’e’) 
  • 求行的加和
rowSums(x) 
  • 資料讀取

常用read.table() 和reas.csv()

read.table(file, header = FALSE, sep = "", quote = "\"'",
           dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
           row.names, col.names, as.is = !stringsAsFactors,
           na.strings = "NA", colClasses = NA, nrows = -1,
           skip = 0, check.names = TRUE, fill = !blank.lines.skip,
           strip.white = FALSE, blank.lines.skip = TRUE,
           comment.char = "#",
           allowEscapes = FALSE, flush = FALSE,
           stringsAsFactors = default.stringsAsFactors(),
           fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", ...)
read.csv(file.choose())  #在檔案目錄中選擇需要的檔案
  • 資料寫入
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
            eol = "\n", na = "NA", dec = ".", row.names = TRUE,
            col.names = TRUE, qmethod = c("escape", "double"),
            fileEncoding = "")

write.csv()和write.csv2()用法與write.table()相似