R의 형식 시스템 개요
double, integer, character, logical, complex 형식을 이해합니다.
R의 형식 시스템 개요은(는) CoddyKit의 무료 R Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 R Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. R Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
R의 유형 시스템
R에는 풍부한 유형 시스템이 있습니다. 모든 객체에는 class(상위 수준의 범주)와 유형(하위 수준의 저장 모드)이 있습니다. 두 가지를 모두 이해하면 올바르고 효율적인 코드를 작성하고 유형 관련 오류를 디버깅하는 데 도움이 됩니다.
# Check class and type of various objects
cat('class(42L) :', class(42L), '
')
cat('typeof(42L) :', typeof(42L), '
')
cat('class(3.14) :', class(3.14), '
')
cat('typeof(3.14) :', typeof(3.14), '
')
cat('class(TRUE) :', class(TRUE), '
')
cat('class("hello"):', class('hello'), '
')class()와 typeof() 비교
class(x)는 R의 객체 지향 시스템에서 사용하는 상위 수준의 범주를 반환합니다(예: 'integer', 'numeric', 'matrix'). typeof(x)는 하위 수준의 C 저장 유형을 반환합니다(예: 'integer', 'double', 'closure').
m <- matrix(1:4, nrow = 2)
cat('class(matrix) :', class(m), '
') # matrix array
cat('typeof(matrix) :', typeof(m), '
') # integer
f <- factor(c('a', 'b', 'a'))
cat('class(factor) :', class(f), '
') # factor
cat('typeof(factor) :', typeof(f), '
') # integer (stored as int!)
cat('class(list) :', class(list()), '
') # list
cat('typeof(list) :', typeof(list()), '
') # listis.numeric() — 숫자 확인
is.numeric(x)는 integer 값과 double 값 모두에 대해(숫자 행렬에도) TRUE를 반환합니다. 특정 저장 모드가 아니라 숫자 상위 유형인지 확인합니다.
cat('is.numeric(3.14):', is.numeric(3.14), '
') # TRUE (double)
cat('is.numeric(3L): ', is.numeric(3L), '
') # TRUE (integer)
cat('is.numeric(TRUE):', is.numeric(TRUE), '
') # FALSE
cat('is.numeric("3"): ', is.numeric('3'), '
') # FALSE
cat('is.numeric(NA): ', is.numeric(NA), '
') # FALSE (NA is logical)is.integer()와 is.double() 비교
is.integer()는 정수 저장인지 구체적으로 검사합니다(L 접미사 또는 as.integer()로 생성된 경우). is.double()는 부동 소수점(double) 저장인지 검사합니다. 둘 다 numeric의 하위 집합입니다.
x_int <- 5L
x_dbl <- 5.0
cat('--- x_int = 5L ---
')
cat('is.integer:', is.integer(x_int), '
') # TRUE
cat('is.double: ', is.double(x_int), '
') # FALSE
cat('is.numeric:', is.numeric(x_int), '
') # TRUE
cat('--- x_dbl = 5.0 ---
')
cat('is.integer:', is.integer(x_dbl), '
') # FALSE
cat('is.double: ', is.double(x_dbl), '
') # TRUE
cat('is.numeric:', is.numeric(x_dbl), '
') # TRUEis.character() — 문자열 확인
is.character(x)는 x가 character(문자열) 벡터이면 TRUE를 반환합니다. 텍스트여야 하는 입력을 검증하거나 숫자형 NA와 character형 NA를 구분할 때 사용하십시오.
greetings <- c('hello', 'world', 'R')
numbers <- c(1, 2, 3)
numeric_string <- '42'
cat('is.character(greetings): ', is.character(greetings), '
')
cat('is.character(numbers): ', is.character(numbers), '
')
cat('is.character(numeric_string): ', is.character(numeric_string), '
')
cat('is.character(NA_character_): ', is.character(NA_character_), '
')is.logical() — 불리언 확인
is.logical(x)는 x에 logical(불리언) 값이 포함되어 있으면 TRUE를 반환합니다. 유형이 지정되지 않은 NA는 기본적으로 logical 값이라는 점에 유의하십시오.
flags <- c(TRUE, FALSE, TRUE, NA)
cat('is.logical(flags): ', is.logical(flags), '
') # TRUE
cat('is.logical(NA): ', is.logical(NA), '
') # TRUE (default NA is logical)
cat('is.logical(1): ', is.logical(1), '
') # FALSE
cat('is.logical("TRUE"):', is.logical('TRUE'), '
') # FALSE
cat('typeof(NA): ', typeof(NA), '
') # logicalR의 유형 계층
R의 원자 유형은 가장 단순한 유형부터 가장 복잡한 유형까지 다음과 같은 계층을 이룹니다: logical < integer < double < complex < character. 벡터에서 유형을 섞으면 R은 모든 요소를 포함된 유형 중 가장 복잡한 유형으로 강제 변환합니다.
# Type hierarchy in action
cat('c(TRUE, 1L) type: ', typeof(c(TRUE, 1L)), '
') # integer
cat('c(1L, 1.5) type: ', typeof(c(1L, 1.5)), '
') # double
cat('c(1.5, 1+0i) type: ', typeof(c(1.5, 1+0i)), '
') # complex
cat('c(1+0i, "a") type: ', typeof(c(1+0i, 'a')), '
') # character
cat('c(TRUE, "x") type: ', typeof(c(TRUE, 'x')), '
') # character프로그램으로 유형 검사하기
객체 목록을 검사하기 위한 유형 검사 함수를 작성할 수 있습니다. class(), typeof(), length()를 함께 사용하면 모든 객체의 구조를 종합적으로 요약할 수 있습니다.
describe_type <- function(x, label) {
cat(label, ': class =', class(x), ', typeof =', typeof(x),
', length =', length(x), '
')
}
describe_type(42L, 'integer literal')
describe_type(3.14, 'double literal')
describe_type(TRUE, 'logical')
describe_type('hello', 'character')
describe_type(c(1,2,3), 'numeric vector')
describe_type(list(1,'a'), 'list')storage.mode()와 mode()
storage.mode(x)는 typeof(x)와 비슷하지만 S3 명명 관례를 사용합니다(예를 들어 'double'을 반환합니다). mode(x)는 조금 더 거친 분류를 반환하며, integer와 double을 'numeric'으로 묶습니다.
x_int <- 5L
x_dbl <- 5.0
cat('mode(5L): ', mode(x_int), '
') # numeric
cat('mode(5.0): ', mode(x_dbl), '
') # numeric
cat('storage.mode(5L): ', storage.mode(x_int), '
') # integer
cat('storage.mode(5.0):', storage.mode(x_dbl), '
') # double
# mode() lumps int+dbl; storage.mode() / typeof() distinguish them목록과 데이터 프레임의 유형 검사
목록이나 데이터 프레임처럼 더 복잡한 객체의 경우 sapply(df, class)는 각 열에 class()를 적용하여 열 유형을 나타내는 이름 있는 벡터를 제공합니다. 데이터 프레임의 스키마를 빠르게 검사하는 방법입니다.
df <- data.frame(
id = 1:3,
name = c('Alice', 'Bob', 'Carol'),
score = c(88.5, 79.0, 92.3),
passed = c(TRUE, TRUE, TRUE)
)
cat('Column types:
')
print(sapply(df, class))
cat('Column typeof:
')
print(sapply(df, typeof))유형 시스템 요약
R의 유형 검사 도구를 다음과 같이 간단히 정리할 수 있습니다.
class(x)— 상위 수준의 S3 class 이름typeof(x)— 하위 수준의 C 저장 유형is.numeric(x)— integer 또는 double이면 TRUEis.integer(x)— integer 저장일 때만 TRUEis.double(x)— double(부동 소수점)일 때만 TRUEis.character(x)— 문자열이면 TRUEis.logical(x)— TRUE/FALSE/NA이면 TRUE
x <- 42L
cat('class(42L): ', class(x), '
')
cat('typeof(42L): ', typeof(x), '
')
cat('is.numeric(42L): ', is.numeric(x), '
')
cat('is.integer(42L): ', is.integer(x), '
')
cat('is.double(42L): ', is.double(x), '
')
cat('is.character(42L):', is.character(x), '
')빠른 확인
R에서 is.numeric(5L)은 무엇을 반환합니까?
복습: R의 유형 시스템
잘하셨습니다! 이번 레슨의 핵심 내용은 다음과 같습니다.
class(x)는 상위 수준의 유형을 반환하고,typeof(x)는 하위 수준의 저장 유형을 반환합니다.is.numeric()은 integer와 double 모두에 대해 TRUE입니다.is.integer()와is.double()은 두 숫자 하위 유형을 구분합니다.- R의 유형 계층(낮음 → 높음): logical → integer → double → complex → character
- 벡터에서 유형을 섞으면 가장 높은 유형으로 자동 강제 변환됩니다.
sapply(df, class)는 데이터 프레임의 모든 열 유형을 검사합니다.
# Type audit function
audit_types <- function(x, name = 'x') {
cat(name, ': class=', class(x), ', typeof=', typeof(x),
', is.numeric=', is.numeric(x), '
')
}
audit_types(1L, 'integer')
audit_types(1.0, 'double')
audit_types(TRUE, 'logical')
audit_types('hello', 'character')
audit_types(1+2i, 'complex')자주 묻는 질문
“R의 형식 시스템 개요” 강의는 무료인가요?
네 — “R의 형식 시스템 개요” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 R Academy 강의 전체를 잠금 해제할 수 있습니다. R Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“R의 형식 시스템 개요”에서 뭘 배우나요?
double, integer, character, logical, complex 형식을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 R Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
R Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 R Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“R의 형식 시스템 개요” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 R Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 R Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- R의 형식 시스템 개요
- 수치 형식 간 변환
- 논리형과 문자형 변환
- 강제 변환의 함정과 모범 사례