r语言如何查看数据类型
时间: 2023-12-20 16:08:02 浏览: 50
在R语言中,可以使用以下函数查看数据类型:
1. typeof()函数:可以查看变量的基本类型,如integer、double、character等。
```{r}
x <- "hello"
typeof(x) # character
y <- 10
typeof(y) # double
```
2. class()函数:可以查看变量的类别,如numeric、character、factor等。
```{r}
x <- "hello"
class(x) # character
y <- factor(c("A", "B", "C"))
class(y) # factor
```
3. str()函数:可以查看变量的结构,包括所有的属性和元素。
```{r}
x <- list(a = 1:3, b = "hello", c = TRUE)
str(x)
# List of 3
# $ a: int [1:3] 1 2 3
# $ b: chr "hello"
# $ c: logi TRUE
```
4. is()函数:可以判断变量是否为某种类型。
```{r}
x <- "hello"
is.character(x) # TRUE
is.numeric(x) # FALSE
```
5. sapply()函数:可以查看数据集中每列的数据类型。
```{r}
data(iris)
sapply(iris, class)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# "numeric" "numeric" "numeric" "numeric" "factor"
```
这些函数可以帮助您查看变量或数据集中各个变量的数据类型。