데이터프레임 생성하기

- 데이터프레임(dataframe): R의 대표적인 데이터 구조, 행과 열로 구성된 테이블 형태로 데이터를 저장할 때 활용

- 아래의 표(iris 데이터 일부)와 같은 형태의 데이터 프레임 생성

Petal.Length Petal.Width Species
1.4 0.2 setosa
1.7 0.4 setosa
4.7 1.4 versicolor
4.9 1.5 versicolor
6.0 2.5 virginica

 

방법1. 컬럼 하나씩 생성 후, 데이터프레임 생성

# 컬럼별 변수 생성 
Petal.Length <- c(1.4, 1.7, 4.7, 4.9, 6.0)
Petal.Width <- c(0.2, 0.4, 1.4, 1.5, 2.5)
Species <- c("setosa", "setosa", "versicolor", "versicolor", "virginica")
# 생성한 변수 활용 데이터 프레임 생성 
iris_df <- data.frame(Petal.Length, Petal.Width, Species)
iris_df

 

 

방법2. 한 번에 데이터프레임 생성

iris_df2 <- data.frame(Petal.Length = c(1.4, 1.7, 4.7, 4.9, 6.0),
                       Petal.Width = c(0.2, 0.4, 1.4, 1.5, 2.5),
                       Species = c("setosa", "setosa", "versicolor", "versicolor", "virginica"))
iris_df2

 

 

+ Recent posts