R 프로그래밍 언어 통계 소프트웨어 및 데이터 분석 도구로 널리 사용되는 오픈 소스 프로그래밍 언어입니다. R 언어의 데이터 프레임 표 형식 데이터를 저장하는 데 사용되는 R의 일반 데이터 개체입니다.
데이터 프레임은 각 열이 있는 행렬로 해석될 수도 있습니다. 행렬 데이터 유형이 다를 수 있습니다. R DataFrame은 데이터, 행, 열이라는 세 가지 주요 구성 요소로 구성됩니다.
R 데이터 프레임 구조
아래 이미지에서 볼 수 있듯이 데이터 프레임의 구조는 다음과 같습니다.
데이터는 표 형식으로 제공되므로 보다 쉽게 작동하고 이해할 수 있습니다.

R – 데이터 프레임
R 프로그래밍 언어로 데이터프레임 생성
R 데이터 프레임을 생성하려면 다음을 사용하십시오. 데이터.프레임() 함수를 생성한 다음 생성한 각 벡터를 인수로 함수에 전달합니다.
아르 자형
# R program to create dataframe> # creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # print the data frame> print>(friend.data)> |
>
>
산출:
friend_id friend_name 1 1 Sachin 2 2 Sourav 3 3 Dravid 4 4 Sehwag 5 5 Dhoni>
R 데이터 프레임의 구조 가져오기
다음을 사용하여 R 데이터 프레임의 구조를 얻을 수 있습니다. str() R의 함수
중첩된 큰 목록의 내부 구조도 표시할 수 있습니다. 이는 사용자에게 객체와 그 구성 요소에 대해 알 수 있도록 기본 R 객체에 대한 한 줄짜리 출력을 제공합니다.
아르 자형
# R program to get the> # structure of the data frame> # creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # using str()> print>(>str>(friend.data))> |
자바 tostring
>
>
산출:
'data.frame': 5 obs. of 2 variables: $ friend_id : int 1 2 3 4 5 $ friend_name: chr 'Sachin' 'Sourav' 'Dravid' 'Sehwag' ... NULL>
R 데이터 프레임의 데이터 요약
R 데이터 프레임에서는 다음을 적용하여 데이터의 통계적 요약과 성격을 얻을 수 있습니다. 요약() 기능.
다양한 모델 피팅 함수의 결과 요약을 생성하는 데 사용되는 일반 함수입니다. 이 함수는 첫 번째 인수의 클래스에 의존하는 특정 메서드를 호출합니다.
아르 자형
# R program to get the> # summary of the data frame> # creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # using summary()> print>(>summary>(friend.data))> |
컴퓨터 네트워크의 네트워크 계층
>
>
산출:
friend_id friend_name Min. :1 Length:5 1st Qu.:2 Class :character Median :3 Mode :character Mean :3 3rd Qu.:4 Max. :5>
R의 데이터 프레임에서 데이터 추출
R 데이터 프레임에서 데이터를 추출한다는 것은 해당 행이나 열에 액세스한다는 의미입니다. 열 이름을 사용하여 R 데이터 프레임에서 특정 열을 추출할 수 있습니다.
아르 자형
# R program to extract> # data from the data frame> # creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # Extracting friend_name column> result <->data.frame>(friend.data$friend_name)> print>(result)> |
>
>
산출:
friend.data.friend_name 1 Sachin 2 Sourav 3 Dravid 4 Sehwag 5 Dhoni>
R 언어에서 데이터 프레임 확장
R의 데이터 프레임은 기존 R 데이터 프레임에 새로운 열과 행을 추가하여 확장할 수 있습니다.
아르 자형
# R program to expand> # the data frame> # creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # Expanding data frame> friend.data$location <->c>(>'Kolkata'>,>'Delhi'>,> >'Bangalore'>,>'Hyderabad'>,> >'Chennai'>)> resultant <- friend.data> # print the modified data frame> print>(resultant)> |
>
>
산출:
friend_id friend_name location 1 1 Sachin Kolkata 2 2 Sourav Delhi 3 3 Dravid Bangalore 4 4 Sehwag Hyderabad 5 5 Dhoni Chennai>
R에서는 다음과 같은 데이터 프레임에 대해 다양한 유형의 작업을 수행할 수 있습니다. 행과 열 액세스, 데이터 프레임의 하위 집합 선택, 데이터 프레임 편집, 데이터 프레임에서 행과 열 삭제 , 등.
참고하세요 R의 DataFrame 작업 데이터 프레임에서 수행할 수 있는 모든 유형의 작업에 대해 알아봅니다.
R 데이터 프레임의 항목에 액세스
단일을 사용하여 데이터 프레임에서 모든 요소를 선택하고 액세스할 수 있습니다.$>,괄호[ ] or>이중 괄호[[]]>데이터 프레임의 열에 액세스합니다.
아르 자형
# creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # Access Items using []> friend.data[1]> # Access Items using [[]]> friend.data[[>'friend_name'>]]> # Access Items using $> friend.data$friend_id> |
>
>
산출:
friend_id 1 1 2 2 3 3 4 4 5 5 Access Items using [[]] [1] 'Sachin' 'Sourav' 'Dravid' 'Sehwag' 'Dhoni' Access Items using $ [1] 1 2 3 4 5>
행과 열의 양
우리는 희미한 함수를 사용하여 데이터 프레임에서 몇 개의 행과 열이 파싱되는지 확인할 수 있습니다.
아르 자형
미국에는 도시가 몇 개 있나요?
# creating a data frame> friend.data <->data.frame>(> >friend_id =>c>(1:5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,> >'Dravid'>,>'Sehwag'>,> >'Dhoni'>),> >stringsAsFactors =>FALSE> )> # find out the number of rows and clumns> dim>(friend.data)> |
>
>
산출:
[1] 5 2>
R 데이터 프레임에 행과 열 추가
R DataFrame에 행과 열을 쉽게 추가할 수 있습니다. 삽입은 새 DataFrame이 필요 없이 기존 DataFrame을 확장하는 데 도움이 됩니다.
DataFrame에 행과 열을 추가하는 방법을 살펴보겠습니다. 예를 들어:
R 데이터 프레임에 행 추가
데이터 프레임에 행을 추가하려면 내장 함수를 사용할 수 있습니다. r바인드().
다음 예는 R 데이터 프레임에서 rbind()의 작동을 보여줍니다.
아르 자형
# Creating a dataframe representing products in a store> Products <->data.frame>(> >Product_ID =>c>(101, 102, 103),> >Product_Name =>c>(>'T-Shirt'>,>'Jeans'>,>'Shoes'>),> >Price =>c>(15.99, 29.99, 49.99),> >Stock =>c>(50, 30, 25)> )> # Print the existing dataframe> cat>(>'Existing dataframe (Products):
'>)> print>(Products)> # Adding a new row for a new product> New_Product <->c>(104,>'Sunglasses'>, 39.99, 40)> Products <->rbind>(Products, New_Product)> # Print the updated dataframe after adding the new product> cat>(>'
Updated dataframe after adding a new product:
'>)> print>(Products)> |
>
>
산출:
Existing dataframe (Products): Product_ID Product_Name Price Stock 1 101 T-Shirt 15.99 50 2 102 Jeans 29.99 30 3 103 Shoes 49.99 25 Updated dataframe after adding a new product: Product_ID Product_Name Price Stock 1 101 T-Shirt 15.99 50 2 102 Jeans 29.99 30 3 103 Shoes 49.99 25 4 104 Sunglasses 39.99 40>
R 데이터 프레임에 열 추가
데이터 프레임에 열을 추가하려면 내장 함수를 사용할 수 있습니다. cbind().
다음 예는 R 데이터 프레임에서 cbind()의 작동을 보여줍니다. .
아르 자형
# Existing dataframe representing products in a store> Products <->data.frame>(> >Product_ID =>c>(101, 102, 103),> >Product_Name =>c>(>'T-Shirt'>,>'Jeans'>,>'Shoes'>),> >Price =>c>(15.99, 29.99, 49.99),> >Stock =>c>(50, 30, 25)> )> # Print the existing dataframe> cat>(>'Existing dataframe (Products):
'>)> print>(Products)> # Adding a new column for 'Discount' to the dataframe> Discount <->c>(5, 10, 8)># New column values for discount> Products <->cbind>(Products, Discount)> # Rename the added column> colnames>(Products)[>ncol>(Products)] <->'Discount'> # Renaming the last column> # Print the updated dataframe after adding the new column> cat>(>'
Updated dataframe after adding a new column 'Discount':
'>)> print>(Products)> |
>
>
산출:
Existing dataframe (Products): Product_ID Product_Name Price Stock 1 101 T-Shirt 15.99 50 2 102 Jeans 29.99 30 3 103 Shoes 49.99 25 Updated dataframe after adding a new column 'Discount': Product_ID Product_Name Price Stock Discount 1 101 T-Shirt 15.99 50 5 2 102 Jeans 29.99 30 10 3 103 Shoes 49.99 25 8>
행 및 열 제거
R의 데이터 프레임은 기존 R 데이터 프레임에서 열과 행을 제거합니다.
R DataFrame에서 행 제거
아르 자형
러드야드 키플링의 요약
library>(dplyr)> # Create a data frame> data <->data.frame>(> >friend_id =>c>(1, 2, 3, 4, 5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,>'Dravid'>,>'Sehwag'>,>'Dhoni'>),> >location =>c>(>'Kolkata'>,>'Delhi'>,>'Bangalore'>,>'Hyderabad'>,>'Chennai'>)> )> data> # Remove a row with friend_id = 3> data <->subset>(data, friend_id != 3)> data> |
>
>
산출:
friend_id friend_name location 1 1 Sachin Kolkata 2 2 Sourav Delhi 3 3 Dravid Bangalore 4 4 Sehwag Hyderabad 5 5 Dhoni Chennai # Remove a row with friend_id = 3 friend_id friend_name location 1 1 Sachin Kolkata 2 2 Sourav Delhi 4 4 Sehwag Hyderabad 5 5 Dhoni Chennai>
위 코드에서는 먼저 다음과 같은 데이터 프레임을 만들었습니다. 데이터 세 개의 열이 있습니다. 친구_ID , 친구_이름 , 그리고 위치 . 다음을 사용하여 행을 제거하려면 친구_ID 3과 같으므로 우리는 부분집합() 함수를 사용하고 조건을 지정했습니다. 친구_ID != 3 . 이로 인해 행이 제거되었습니다. 친구_ID 3과 같습니다.
R DataFrame에서 열 제거
아르 자형
library>(dplyr)> # Create a data frame> data <->data.frame>(> >friend_id =>c>(1, 2, 3, 4, 5),> >friend_name =>c>(>'Sachin'>,>'Sourav'>,>'Dravid'>,>'Sehwag'>,>'Dhoni'>),> >location =>c>(>'Kolkata'>,>'Delhi'>,>'Bangalore'>,>'Hyderabad'>,>'Chennai'>)> )> data> # Remove the 'location' column> data <->select>(data, -location)> data> |
>
>
산출:
friend_id friend_name location 1 1 Sachin Kolkata 2 2 Sourav Delhi 3 3 Dravid Bangalore 4 4 Sehwag Hyderabad 5 5 Dhoni Chennai>'location' 열 제거 friend_id friend_name 1 1 Sachin 2 2 Sourav 3 3 Dravid 4 4 Sehwag 5 5 Dhoni>
제거하려면 위치 열에서 우리는 선택하다() 기능 및 지정 -위치 . 그만큼 – 기호는 우리가 제거하고 싶다는 것을 나타냅니다. 위치 열. 결과 데이터 프레임 데이터 두 개의 열만 있습니다. 친구_ID 그리고 친구_이름 .
R에서 데이터 프레임 결합
R에는 데이터 프레임을 결합하는 방법이 2가지 있습니다. 수직 또는 수평으로 결합할 수 있습니다.
예를 들어 두 경우를 모두 살펴보겠습니다.
R 데이터 프레임을 수직으로 결합
2개의 데이터 프레임을 수직으로 결합하려면 다음을 사용할 수 있습니다. rbind() 함수. 이 기능은 두 개 이상의 데이터 프레임을 조합하는 데 사용됩니다.
아르 자형
# Creating two sample dataframes> df1 <->data.frame>(> >Name =>c>(>'Alice'>,>'Bob'>),> >Age =>c>(25, 30),> >Score =>c>(80, 75)> )> df2 <->data.frame>(> >Name =>c>(>'Charlie'>,>'David'>),> >Age =>c>(28, 35),> >Score =>c>(90, 85)> )> # Print the existing dataframes> cat>(>'Dataframe 1:
'>)> print>(df1)> cat>(>'
Dataframe 2:
'>)> print>(df2)> # Combining the dataframes using rbind()> combined_df <->rbind>(df1, df2)> # Print the combined dataframe> cat>(>'
Combined Dataframe:
'>)> print>(combined_df)> |
>
MVC 자바
>
산출:
Dataframe 1: Name Age Score 1 Alice 25 80 2 Bob 30 75 Dataframe 2: Name Age Score 1 Charlie 28 90 2 David 35 85 Combined Dataframe: Name Age Score 1 Alice 25 80 2 Bob 30 75 3 Charlie 28 90 4 David 35 85>
R 데이터 프레임을 수평으로 결합:
2개의 데이터 프레임을 수평으로 결합하려면 다음을 사용할 수 있습니다. cbind() 함수. 이 기능은 두 개 이상의 데이터 프레임을 조합하는 데 사용됩니다.
아르 자형
# Creating two sample dataframes> df1 <->data.frame>(> >Name =>c>(>'Alice'>,>'Bob'>),> >Age =>c>(25, 30),> >Score =>c>(80, 75)> )> df2 <->data.frame>(> >Height =>c>(160, 175),> >Weight =>c>(55, 70)> )> # Print the existing dataframes> cat>(>'Dataframe 1:
'>)> print>(df1)> cat>(>'
Dataframe 2:
'>)> print>(df2)> # Combining the dataframes using cbind()> combined_df <->cbind>(df1, df2)> # Print the combined dataframe> cat>(>'
Combined Dataframe:
'>)> print>(combined_df)> |
>
>
산출:
Dataframe 1: Name Age Score 1 Alice 25 80 2 Bob 30 75 Dataframe 2: Height Weight 1 160 55 2 175 70 Combined Dataframe: Name Age Score Height Weight 1 Alice 25 80 160 55 2 Bob 30 75 175 70>
또한 읽어보세요:
- R – 객체
- R 프로그래밍의 데이터 구조
이 기사에서 우리는 다뤘습니다. R 데이터 프레임, 생성, 액세스, 요약, 추가 및 제거와 같은 모든 기본 작업이 포함됩니다. 이 문서의 목적은 프로젝트에서 사용할 수 있도록 R의 데이터 프레임에 익숙해지는 것입니다.
이것이 R의 데이터 프레임 개념을 이해하는 데 도움이 되기를 바라며 프로젝트에서 R 데이터 프레임을 쉽게 구현할 수 있기를 바랍니다.