R語言數(shù)據(jù)框中的負索引介紹
以R語言自帶的mtcars數(shù)據(jù)框為例:
這是原始的mtcars數(shù)據(jù):
這里只列出了前面幾行數(shù)據(jù)。
然后負索引mtcars[,-2:-3],得到的結(jié)果
刪除了第二列和第三列數(shù)據(jù)
所以R語言數(shù)據(jù)框中的負索引是指刪除數(shù)據(jù)框中對應(yīng)的列(或者行)
ps:這和Python里面的規(guī)則好像不太一樣,Python里的負索引好像是指倒數(shù)第幾列(或者第幾行),這里這兩個軟件區(qū)別還挺大的~~寫個筆記提醒一下自己~
補充:R語言中的負整數(shù)索引
看代碼吧~
> x<-matrix(c(1,2,3,4,5,6,7,8,9),nrow = 3,ncol = 3,byrow = TRUE) > x [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 > x[-1,] [,1] [,2] [,3] [1,] 4 5 6 [2,] 7 8 9
這在R中稱為負整數(shù)索引向量,這種索引向量指定被排除的元素而不是包括進來,因此x[-1,]表示取出矩陣x的除了第一行元素外的其他元素。
補充:R語言-基本語法、數(shù)據(jù)類型及索引
1. 基本語法
print()
、cat()
打印輸出
#
單行注釋
if(FALSE){code block}
多行注釋
2. 數(shù)據(jù)類型
class():查看數(shù)據(jù)類型
2.1 基本數(shù)據(jù)類型
基本數(shù)據(jù)類型 | 示例 |
---|---|
邏輯值(logical) | 真:TRUE、T,假:FALSE、F |
數(shù)字(numeric) | 123、5 |
整型(integer) | 2L、34L |
復(fù)數(shù)(complex) | 3+2i |
字符(character) | 'good' |
2.2 向量Vector
c()函數(shù)創(chuàng)建向量。
注意:必須保證元素類型相同,否則會默認進行類型轉(zhuǎn)換。
> x <- c(1, 2) > class(x) [1] "numeric" > x <- c('s') > class(x) [1] "character" > x <- c(1, 2, 's') > class(x) [1] "character"
2.3 列表List
列表可以包含許多不同類型的元素,如向量、函數(shù)、嵌套列表。
注意:[]與[[]]的區(qū)別。[]取出來的仍是一個列表,[[]]取出來的是本身的數(shù)據(jù)類型。
> list1 <- list(c(2,3), 21, 's', sin) # 分別包含列表、數(shù)字、字符、函數(shù) > class(list1) [1] "list" > list1[1] # 取出來的仍是一個列表 [[1]] [1] 2 3 > list1[[1]] # 取出來的是子列表中的元素 [1] 2 3 > class(list1[1]) [1] "list" > class(list1[[1]]) [1] "numeric" > list1[[2]] [1] 21 > list1[2] + 2 Error in list1[2] + 2 : non-numeric argument to binary operator > list1[[2]] + 2 [1] 23 > list1[[4]] function (x) .Primitive("sin") > class(list1[[4]]) [1] "function"
2.4 矩陣Matrix
矩陣是二維數(shù)據(jù)集,它可以使用矩陣函數(shù)的向量輸入創(chuàng)建。
byrow參數(shù)決定元素存放的順序。
> M <- matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) > M [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a" > M[,1] # 取出第一列數(shù)據(jù) [1] "a" "c" > M[1,] # 取出第一行數(shù)據(jù) [1] "a" "a" "b" > M[2,1] # 取出單個元素 [1] "c"
2.5 數(shù)組Array
利用數(shù)組可以創(chuàng)建任意維度的數(shù)據(jù)。
> array1 <- array(c('green','yellow'), dim=c(3,3,2)) > array1 , , 1 [,1] [,2] [,3] [1,] "green" "yellow" "green" [2,] "yellow" "green" "yellow" [3,] "green" "yellow" "green" , , 2 [,1] [,2] [,3] [1,] "yellow" "green" "yellow" [2,] "green" "yellow" "green" [3,] "yellow" "green" "yellow"
2.6 因子Factor
因子是使用向量創(chuàng)建的對象。它將向量與向量中元素的不同值一起存儲為標簽。 標簽是字符類型。 它們在統(tǒng)計建模中非常有用。
使用factor()函數(shù)創(chuàng)建因子。nlevels函數(shù)給出級別計數(shù)。
> apple_colors <- c('green','green','yellow','red','red','red','green') > factor_apple <- factor(apple_colors) > factor_apple [1] green green yellow red red red green Levels: green red yellow > nlevels(factor_apple) [1] 3
2.7 數(shù)據(jù)框Data Frame
表格數(shù)據(jù)對象。每列可以包含不同的數(shù)據(jù)類型。 第一列可以是數(shù)字,而第二列可以是字符,第三列可以是邏輯的。 它是等長度的向量的列表。
使用data.frame()函數(shù)創(chuàng)建數(shù)據(jù)框。
# 創(chuàng)建數(shù)據(jù)框,表格對象 > BMI <- data.frame( gender = c("Male", "Male","Female"), height = c(152, 171.5, 165), weight = c(81,93, 78), Age = c(42,38,26) ) > BMI gender height weight Age 1 Male 152.0 81 42 2 Male 171.5 93 38 3 Female 165.0 78 26 # 獲取第二列 > BMI[2] height 1 152.0 2 171.5 3 165.0 # 獲取第一行 > BMI[1,] gender height weight Age 1 Male 152 81 42 # 獲取第一列數(shù)據(jù),類型為DataFrame > BMI[1] gender 1 Male 2 Male 3 Female > class(BMI[1]) [1] "data.frame" # 獲取第一列,并將其轉(zhuǎn)換為factor類型 > BMI[,1] [1] Male Male Female Levels: Female Male # 獲取第一個元素,轉(zhuǎn)換為factor類型 > BMI[1,1] [1] Male Levels: Female Male # 獲取第二列,不改變數(shù)據(jù)類型 > BMI[2] height 1 152.0 2 171.5 3 165.0 # 獲取第二列,改變數(shù)據(jù)類型 > BMI[,2] [1] 152.0 171.5 165.0 # 根據(jù)列的名稱獲取factor類型數(shù)據(jù) data_frame$col_name
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
R語言數(shù)據(jù)可視化ggplot添加左右y軸繪制天貓雙十一銷售圖
本篇文章主要介紹如何在R中,使用ggplot2包在一個圖像上添加左右兩個 y 軸刻度,并在同一個圖像上繪制兩個完全不一樣的統(tǒng)計圖,有需要的朋友可以借鑒參考下2021-11-11R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式
這篇文章主要介紹了R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言數(shù)據(jù)可視化繪圖Slope chart坡度圖畫法
這篇文章主要為大家介紹了R語言數(shù)據(jù)可視化繪圖Slope?chart坡度圖的畫法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-02-02