How to view data types in R language?

In R language, you can use the typeof() function and class() function to check the data type.

The typeof() function can return a character vector that identifies the data object type, including:

  1. “logical” : characterized by logic
  2. “integer”: whole number
  3. “double”: double precision type
  4. plural form
  5. “character” can be translated as “character type” in English.
  6. “raw” : primitive type
  7. “List”: list type
  8. “NULL”: empty object

Example code:

x <- 10
typeof(x) # 返回 "double"

y <- "hello"
typeof(y) # 返回 "character"

The class() function returns the category of a data object and can also be used to check the type of data. It can provide more specific information about the type, such as:

  1. “numeric”: numerical type (including integer and double type)
  2. “factor” can be translated as “factor type” in English.
  3. matrix: matrix type
  4. data frame: table data structure
  5. “POSIXct”: type for date and time.

Example code:

x <- 10
class(x) # 返回 "numeric"

y <- factor(c("a", "b", "c"))
class(y) # 返回 "factor"

It is important to note that the results returned by the typeof() and class() functions may differ. For example, for vectors and numeric objects, the typeof() function may return “double”, but the class() function may return “numeric” or “integer”. This is because the class() function can provide more specific category information, while the typeof() function only provides basic data type information.

广告
Closing in 10 seconds
bannerAds