HOWTO · R

R에서 변수 데이터 유형 확인

이 튜토리얼은 R에서 변수 유형을 확인하는 방법을 보여줍니다.

다른 데이터 유형에 대한 변수의 데이터 유형을 확인하는 다른 방법이 있습니다. 이 튜토리얼은 R에서 변수의 데이터 유형을 확인하는 방법을 보여줍니다.

R에서 변수 데이터 유형 확인

R의 많은 방법을 사용하여 변수의 데이터 유형을 확인할 수 있습니다. 이러한 방법은 아래에 나와 있습니다.

# for checking data type of one variable
typeof(x)
class(x)

# for checking the data type of every variable in a data frame
str(dataframe)

# for checking if a variable is a specific data type
is.factor(x)
is.numeric(x)
is.logical(x)
is.complex(x)
is.integer()

각각의 방법을 하나씩 시도해보자.

R에서 한 변수의 데이터 유형 확인

단일 변수 또는 객체의 데이터 유형을 확인하는 방법에는 typeof() 메서드와 class() 메서드의 두 가지가 있습니다. 두 메서드 모두 변수 또는 개체인 하나의 매개 변수를 사용합니다.

두 가지 방법 모두에 대해 한 가지 예를 시도해 보겠습니다.

#define variable Demo
Demo <- c("Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6")

#check the data type of Demo using typeof method
print('The output for typeof method:')
typeof(Demo)

#check the data type of Demo using the class method
print('The output for class method:')
class(Demo)

위의 코드는 typeof() 메서드를 사용하여 변수 유형을 확인한 다음 class() 메서드를 사용하여 변수 유형을 확인합니다. 출력 참조:

[1] "The output for typeof method:"
[1] "character"

[1] "The output for class method:"
[1] "character"

R의 데이터 프레임에 있는 모든 변수의 데이터 유형 확인

str() 메소드는 데이터 프레임에서 각 변수의 데이터 유형을 확인하는 데 사용됩니다. 데이터 프레임인 하나의 매개변수를 사용합니다.

예를 참조하십시오.

#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
                         LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
                         Id=c(101, 102, 103, 104, 105),
                         Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))

#View the dataframe
Delftstack

#Check the data type of every variable in dataframe
str(Delftstack)

위의 코드는 데이터 프레임의 각 변수에 대한 데이터 유형을 확인합니다. 출력 참조:

      Name LastName  Id     Designation
1     Jack  Danials 101             CEO
2     John     Cena 102 Project Manager
3     Mike Chandler 103      Senior Dev
4 Michelle   McCool 104      Junior Dev
5   Jhonny    Nitro 105          Intern

'data.frame':	5 obs. of  4 variables:
 $ Name       : chr  "Jack" "John" "Mike" "Michelle" ...
 $ LastName   : chr  "Danials" "Cena" "Chandler" "McCool" ...
 $ Id         : num  101 102 103 104 105
 $ Designation: chr  "CEO" "Project Manager" "Senior Dev" "Junior Dev" ...

R에서 변수가 특정 데이터 유형인지 확인

변수가 특정 유형인지 확인하는 몇 가지 방법이 있습니다. 각 데이터 유형에 대해 한 가지 방법이 있습니다. 각 메소드는 변수를 매개변수로 사용하고 True 또는 False를 반환합니다.

각 방법을 보여주기 위해 예제를 시도해보자.

#create variables
a <- 3

b <- 5.3

c <- "Delftstack"

d <- TRUE

e <- factor(c('A', 'B', 'C', 'D'))

i <- as.integer(a)


## Check types of variables

print(is.numeric(a))

print(is.complex(b))

print(is.character(c))

print(is.logical(d))

print(is.factor(e))

print(is.integer(i))

위의 코드는 각 유형에 대한 변수를 생성하고 해당 데이터 유형이 있는지 확인합니다. 출력 참조:

> ## Check types of variables

[1] TRUE

[1] FALSE

[1] TRUE

[1] TRUE

[1] TRUE

[1] TRUE

코드는 b 변수가 복합 유형 변수가 아님을 보여줍니다. 다른 모든 변수는 메소드와 동일한 데이터 유형을 갖습니다.

또한 이러한 방법을 사용하여 데이터 프레임의 열에 대한 데이터 유형을 확인할 수 있습니다. 예를 참조하십시오.

#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
                         LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
                         Id=c(101, 102, 103, 104, 105),
                         Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))


#check if Name column is character
is.character(Delftstack$Name)

#check if LastName column is complex
is.complex(Delftstack$LastName)

#check if the Id column is numeric
is.numeric(Delftstack$Id)

위의 코드는 데이터 프레임의 열이 특정 유형의 데이터인지 여부를 확인합니다. 출력 참조:

> #check if Name column is character
[1] TRUE

> #check if LastName column is complex
[1] FALSE

> #check if Id column is numeric
[1] TRUE