R에서 빈 데이터 프레임 만들기

Sheeraz Gul 2023년6월21일
  1. 행렬 생성 및 R의 데이터 프레임으로 변환
  2. 빈 벡터를 초기화하여 R에서 빈 데이터 프레임 만들기
R에서 빈 데이터 프레임 만들기

빈 데이터 프레임은 축의 길이가 0인 테이블 구조를 의미합니다. 빈 데이터 프레임을 만드는 방법에는 두 가지가 있습니다.

이 튜토리얼은 R에서 빈 데이터 프레임을 만드는 방법을 보여줍니다.

행렬 생성 및 R의 데이터 프레임으로 변환

행렬과 데이터 프레임은 서로 쉽게 변환할 수 있습니다. 빈 데이터 프레임을 만들기 위해 열과 행 모두에 대해 길이가 0인 빈 행렬을 만들고 데이터 프레임으로 변환합니다.

예를 들어 보겠습니다.

# create a matrix
demo_matrix = matrix(ncol = 0, nrow = 0)

# convert the matrix to data frame
delftstack=data.frame(demo_matrix)
# Print data frame
print(delftstack)

# dimensions of the data frame
print("The Dimensions of the data frame")
dim(delftstack)

위의 코드는 빈 행렬을 만들고 데이터 프레임으로 변환한 다음 마지막으로 차원을 표시합니다. 출력 참조:

data frame with 0 columns and 0 rows

[1] "The Dimensions of the data frame"
[1] 0 0

빈 벡터를 인수로 전달할 수 있기 때문에 열 이름을 지정하지 않고 빈 데이터 프레임을 만들 수도 있습니다.

# declare empty data frame with 4 columns and null entries
delftstack = data.frame(matrix( vector(), 0, 4, dimnames=list(c(), c("C1","C2","C3","C4"))),
  stringsAsFactors=F)

# print the data frame
print ("The Empty dataframe is")
print (delftstack)

이 코드는 4개의 빈 열이 있는 빈 데이터 프레임을 만듭니다. 출력 참조:

[1] "The Empty dataframe is"
[1] C1 C2 C3 C4
<0 rows> (or 0-length row.names)

빈 벡터를 초기화하여 R에서 빈 데이터 프레임 만들기

이 메서드는 데이터 프레임을 클래스 유형이 있는 빈 벡터 집합으로 정의합니다. 모든 문자 벡터를 문자열로 처리할 수 있도록 stringsAsFactors=False를 지정해야 합니다.

예를 참조하십시오:

#data frame with 5 empty vectors
delftstack <- data.frame(Doubles=double(),
                  Integers=integer(),
                  Factors=factor(),
                  Logicals=logical(),
                  Characters=character(),
                  stringsAsFactors=FALSE)

#the data frame structure
str(delftstack)

위의 코드는 5개의 빈 벡터가 있는 빈 데이터 프레임을 만든 다음 데이터 프레임의 구조를 보여줍니다. 출력 참조:

'data.frame':	0 obs. of  5 variables:
 $ Doubles   : num
 $ Integers  : int
 $ Factors   : Factor w/ 0 levels:
 $ Logicals  : logi
 $ Characters: chr
작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

관련 문장 - R Data Frame