HOWTO · R
R에서 두 열 연결
이 튜토리얼은 R에서 두 열을 연결하는 방법을 보여줍니다.
이 페이지의 내용
경우에 따라 한 열의 값을 다른 열의 값에 연결해야 합니다. R에는 두 열을 연결하는 두 가지 방법이 있습니다. 이 튜토리얼은 R에서 데이터 프레임 열을 연결하는 두 가지 방법을 보여줍니다.
Paste() 함수를 사용하여 R에서 두 열 연결
Paste()는 데이터 프레임에서 두 열을 연결할 수 있는 기본 R의 기본 제공 함수입니다. 새 열을 만들고 매개 변수로 전달된 두 열을 구분 기호와 결합합니다.
예를 참조하십시오:
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'))
print('The dataframe before concatenating the columns:-')
Delftstack
#combine name and ID into one column
Delftstack$Id_Name <- paste(Delftstack$Id, Delftstack$Name, sep="_")
print('The dataframe after concatenating the columns:-')
Delftstack
위의 코드는 Id 및 Name 열을 새 열 Id_Name으로 연결합니다. 새 열을 만든 다음 열 이름 및 구분 기호와 함께 붙여넣기 기능을 사용하여 두 열을 연결합니다.
열 이름은 datframe$columnname으로 표시됩니다. 출력을 참조하십시오.
[1] "The dataframe before concatenating the columns:-"
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
[1] "The dataframe after concatenating the columns:-"
Name LastName Id Designation Id_Name
1 Jack Danials 101 CEO 101_Jack
2 John Cena 102 Project Manager 102_John
3 Mike Chandler 103 Senior Dev 103_Mike
4 Michelle McCool 104 Junior Dev 104_Michelle
5 Jhonny Nitro 105 Intern 105_Jhonny
원하는 경우 연결 후 이전 열을 제거할 수 있습니다.
New_Delftstack <- Delftstack[c("Id_Name", "LastName", "Designation")]
print('The dataframe after concatenating the columns:-')
New_Delftstack
위의 코드는 이전 열을 제거하고 연결된 열이 있는 새 데이터 프레임을 제공합니다. 출력 참조:
[1] "The dataframe after concatenating the columns:-"
Id_Name LastName Designation
1 101_Jack Danials CEO
2 102_John Cena Project Manager
3 103_Mike Chandler Senior Dev
4 104_Michelle McCool Junior Dev
5 105_Jhonny Nitro Intern
Tidyr의 Unite() 함수를 사용하여 R에서 두 열을 연결합니다.
R의 tidyr 패키지에는 R에서 두 개의 열을 연결할 수 있는 unite() 함수가 있습니다. 데이터 프레임, 새 열 이름 및 매개 변수로 연결된 열을 사용합니다.
예를 참조하십시오:
library(tidyr)
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'))
print('The dataframe before concatenating the columns:-')
Delftstack
print('The dataframe after concatenating the columns:-')
unite(Delftstack, Id_Name, c(Id, Name))
위의 코드는 두 열을 연결하고 이전 열을 제거합니다. 출력 참조:
[1] "The dataframe before concatenating the columns:-"
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
[1] "The dataframe after concatenating the columns:-"
Id_Name LastName Designation
1 101_Jack Danials CEO
2 102_John Cena Project Manager
3 103_Mike Chandler Senior Dev
4 104_Michelle McCool Junior Dev
5 105_Jhonny Nitro Intern
열을 제거하는 경우에는 unite()가 더 편리함을 알 수 있습니다.