HOWTO · R
檢查 R 中的變數資料型別
本教程演示如何在 R 中檢查變數型別。
本頁內容
有不同的方法來檢查不同資料型別的變數的資料型別。本教程演示如何檢查 R 中變數的資料型別。
檢查 R 中的變數資料型別
R 中的許多方法可用於檢查變數的資料型別。這些方法在下面給出。
# for checking data type of one variable
typeof(x)
class(x)
# for checking the data type of every variable in a data frame
str(dataframe)
# for checking if a variable is a specific data type
is.factor(x)
is.numeric(x)
is.logical(x)
is.complex(x)
is.integer()
讓我們一一嘗試每種方法。
檢查 R 中一個變數的資料型別
有兩種方法可以檢查單個變數或物件的資料型別,typeof() 方法和 class() 方法。這兩種方法都採用一個引數,即變數或物件。
讓我們為這兩種方法嘗試一個示例。
#define variable Demo
Demo <- c("Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6")
#check the data type of Demo using typeof method
print('The output for typeof method:')
typeof(Demo)
#check the data type of Demo using the class method
print('The output for class method:')
class(Demo)
上面的程式碼將使用 typeof() 方法檢查變數型別,然後使用 class() 方法。見輸出:
[1] "The output for typeof method:"
[1] "character"
[1] "The output for class method:"
[1] "character"
檢查 R 中資料框中每個變數的資料型別
str() 方法用於檢查資料框中每個變數的資料型別。它接受一個引數,即資料幀。
參見示例:
#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
Id=c(101, 102, 103, 104, 105),
Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))
#View the dataframe
Delftstack
#Check the data type of every variable in dataframe
str(Delftstack)
上面的程式碼檢查資料框中每個變數的資料型別。見輸出:
Name LastName Id Designation
1 Jack Danials 101 CEO
2 John Cena 102 Project Manager
3 Mike Chandler 103 Senior Dev
4 Michelle McCool 104 Junior Dev
5 Jhonny Nitro 105 Intern
'data.frame': 5 obs. of 4 variables:
$ Name : chr "Jack" "John" "Mike" "Michelle" ...
$ LastName : chr "Danials" "Cena" "Chandler" "McCool" ...
$ Id : num 101 102 103 104 105
$ Designation: chr "CEO" "Project Manager" "Senior Dev" "Junior Dev" ...
檢查變數是否是 R 中的特定資料型別
有幾種方法可以檢查變數是否屬於特定型別,每種資料型別都有一種方法。每個方法都將變數作為引數並返回 True 或 False。
讓我們嘗試一個示例來展示每種方法。
#create variables
a <- 3
b <- 5.3
c <- "Delftstack"
d <- TRUE
e <- factor(c('A', 'B', 'C', 'D'))
i <- as.integer(a)
## Check types of variables
print(is.numeric(a))
print(is.complex(b))
print(is.character(c))
print(is.logical(d))
print(is.factor(e))
print(is.integer(i))
上面的程式碼為每種型別建立一個變數,並檢查它是否具有相應的資料型別。見輸出:
> ## Check types of variables
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
程式碼顯示變數 b 不是複雜型別變數;所有其他變數都具有與方法相同的資料型別。
我們還可以使用這些方法來檢查資料框列的資料型別。參見示例:
#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
Id=c(101, 102, 103, 104, 105),
Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))
#check if Name column is character
is.character(Delftstack$Name)
#check if LastName column is complex
is.complex(Delftstack$LastName)
#check if the Id column is numeric
is.numeric(Delftstack$Id)
上面的程式碼檢查資料框的一列是否是特定型別的資料。見輸出:
> #check if Name column is character
[1] TRUE
> #check if LastName column is complex
[1] FALSE
> #check if Id column is numeric
[1] TRUE