A daily R language tip (4) - get the data type (single variable, each column in the dataframe data frame, each element)

Knowledge point self-test

  1. What are the object types in R? What are the data types?
  2. How to get the data type of a variable?
  3. How can I get the data type of each column in df like below?
> df=data.frame(1:5,c(T,F,T,F,F),letters[2:6])
> df
  X1.5 c.T..F..T..F..F. letters.2.6.
1    1             TRUE            b
2    2            FALSE            c
3    3             TRUE            d
4    4            FALSE            e
5    5            FALSE            f

Answer

  1. There are seven object types in R
  • vector vector
  • list list
  • matrix matrix
  • array array
  • factor factor
  • data.frame data frame
  • scalar scalar

There are many types of data, we usually come into contact with the following types

  • numeric
  • complex
  • logical
  • character
  • null
  • double
  • list

First understand whether you want an object type or a data type!!!

get data type
  • typeof()
  • mode()
  • storage.mode()
> typeof(df)
[1] "list"
> mode(df)
[1] "list"
> storage.mode(df)
[1] "list"
  1. Use the str() function to easily get the structure of an R object
> str(df)
'data.frame':	5 obs. of  3 variables:
 $ X1.5            : int  1 2 3 4 5
 $ c.T..F..T..F..F.: logi  TRUE FALSE TRUE FALSE FALSE
 $ letters.2.6.    : Factor w/ 5 levels "b","c","d","e",..: 1 2 3 4 5

There is also a function that can also see the detailed information of the R object, but it can only see the object type, and does not display the data type of the specific column

> attributes(df)
$names
[1] "X1.5"             "c.T..F..T..F..F." "letters.2.6."    

$class
[1] "data.frame"

$row.names
[1] 1 2 3 4 5

Guess you like

Origin blog.csdn.net/ptyp222/article/details/114890268