R語言數(shù)據(jù)類型知識點總結(jié)
通常,在使用任何編程語言進行編程時,您需要使用各種變量來存儲各種信息。 變量只是保留值的存儲位置。 這意味著,當你創(chuàng)建一個變量,你必須在內(nèi)存中保留一些空間來存儲它們。
您可能想存儲各種數(shù)據(jù)類型的信息,如字符,寬字符,整數(shù),浮點,雙浮點,布爾等?;谧兞康臄?shù)據(jù)類型,操作系統(tǒng)分配內(nèi)存并決定什么可以存儲在保留內(nèi)存中。
與其他編程語言(如 C 中的 C 和 java)相反,變量不會聲明為某種數(shù)據(jù)類型。 變量分配有 R 對象,R 對象的數(shù)據(jù)類型變?yōu)樽兞康臄?shù)據(jù)類型。盡管有很多類型的 R 對象,但經(jīng)常使用的是:
- 矢量
- 列表
- 矩陣
- 數(shù)組
- 因子
- 數(shù)據(jù)幀
這些對象中最簡單的是向量對象,并且這些原子向量有六種數(shù)據(jù)類型,也稱為六類向量。 其他 R 對象建立在原子向量之上。
數(shù)據(jù)類型 | 例 | 校驗 |
---|---|---|
Logical(邏輯型) | TRUE, FALSE | v <- TRUE print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "logical" |
Numeric(數(shù)字) | 12.3,5,999 | v <- 23.5 print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "numeric" |
Integer(整型) | 2L,34L,0L | v <- 2L print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "integer" |
Complex(復合型) | 3 + 2i | v <- 2+5i print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "complex" |
Character(字符) | 'a' , "good", "TRUE", '23.4' | v <- "TRUE" print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "character" |
Raw(原型) | "Hello" 被存儲為 48 65 6c 6c 6f | v <- charToRaw("Hello") print(class(v))
它產(chǎn)生以下結(jié)果 - [1] "raw" |
在 R 編程中,非常基本的數(shù)據(jù)類型是稱為向量的 R 對象,其保存如上所示的不同類的元素。 請注意,在 R 中,類的數(shù)量不僅限于上述六種類型。 例如,我們可以使用許多原子向量并創(chuàng)建一個數(shù)組,其類將成為數(shù)組。
Vectors 向量
當你想用多個元素創(chuàng)建向量時,你應(yīng)該使用 c() 函數(shù),這意味著將元素組合成一個向量。
# Create a vector. apple <- c('red','green',"yellow") print(apple) # Get the class of the vector. print(class(apple))
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[1] "red" "green" "yellow" [1] "character"
Lists 列表
列表是一個R對象,它可以在其中包含許多不同類型的元素,如向量,函數(shù)甚至其中的另一個列表。
# Create a list. list1 <- list(c(2,5,3),21.3,sin) # Print the list. print(list1)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[[1]] [1] 2 5 3 [[2]] [1] 21.3 [[3]] function (x) .Primitive("sin")
Matrices 矩陣
矩陣是二維矩形數(shù)據(jù)集。 它可以使用矩陣函數(shù)的向量輸入創(chuàng)建。
# Create a matrix. M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) print(M)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a"
Arrays 數(shù)組
雖然矩陣被限制為二維,但陣列可以具有任何數(shù)量的維度。 數(shù)組函數(shù)使用一個 dim 屬性創(chuàng)建所需的維數(shù)。 在下面的例子中,我們創(chuàng)建了一個包含兩個元素的數(shù)組,每個元素為 3x3 個矩陣。
# Create an array. a <- array(c('green','yellow'),dim = c(3,3,2)) print(a)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
, , 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"
Factors 因子
因子是使用向量創(chuàng)建的 r 對象。 它將向量與向量中元素的不同值一起存儲為標簽。 標簽總是字符,不管它在輸入向量中是數(shù)字還是字符或布爾等。 它們在統(tǒng)計建模中非常有用。
使用 factor() 函數(shù)創(chuàng)建因子。nlevels 函數(shù)給出級別計數(shù)。
# Create a vector. apple_colors <- c('green','green','yellow','red','red','red','green') # Create a factor object. factor_apple <- factor(apple_colors) # Print the factor. print(factor_apple) print(nlevels(factor_apple))
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[1] green green yellow red red red green Levels: green red yellow # applying the nlevels function we can know the number of distinct values [1] 3
Data Frames 數(shù)據(jù)幀
數(shù)據(jù)幀是表格數(shù)據(jù)對象。 與數(shù)據(jù)幀中的矩陣不同,每列可以包含不同的數(shù)據(jù)模式。 第一列可以是數(shù)字,而第二列可以是字符,第三列可以是邏輯的。 它是等長度的向量的列表。
使用 data.frame() 函數(shù)創(chuàng)建數(shù)據(jù)幀。
# Create the data frame. BMI <- data.frame( gender = c("Male", "Male","Female"), height = c(152, 171.5, 165), weight = c(81,93, 78), Age = c(42,38,26) ) print(BMI)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
gender height weight Age 1 Male 152.0 81 42 2 Male 171.5 93 38 3 Female 165.0 78 26
到此這篇關(guān)于R語言數(shù)據(jù)類型知識點總結(jié)的文章就介紹到這了,更多相關(guān)R語言數(shù)據(jù)類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!