티스토리 뷰

Data Science/R package

Basic data types

jsBae 2022. 5. 2. 23:36
  • numeric - (10.5, 55, 787)
  • integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
  • complex - (9 + 3i, where "i" is the imaginary part)
  • character (a.k.a. string) - ("k", "R is exciting", "FALSE", "11.5")
  • logical (a.k.a. boolean) - (TRUE or FALSE)

type 확인 : class()

> # numeric
> x <- 10.5
> class(x)
[1] "numeric"
> 
> # integer
> x <- 1000L
> class(x)
[1] "integer"
> 
> # complex
> x <- 9i + 3
> class(x)
[1] "complex"
> 
> # character/string
> x <- "R is exciting"
> class(x)
[1] "character"
> 
> # logical/boolean
> x <- TRUE
> class(x)
[1] "logical"

R Numbers

x <- 10.5   # numeric
y <- 10L    # integer
z <- 1i     # complex

data type 변환

> x <- 1L # integer
> y <- 2 # numeric
> 
> # convert from integer to numeric:
> a <- as.numeric(x)
> 
> # convert from numeric to integer:
> b <- as.integer(y)
> 
> # print values of x and y
> x
[1] 1
> y
[1] 2
> 
> # print the class name of a and b
> class(a)
[1] "numeric"
> class(b)
[1] "integer"
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
반응형