sid
sid

Reputation: 113

Sorting dataframe

I have two dataframes of same dimensions. But as shown I want to order the dataframe datasuch that the id_num is ordered in the order of id_num of reference. The output should look like the dataframe required.

How can I do it?

reference <- data.frame(id_num=c(206,307,408,506), Event_1=0, Event_2=0)

enter image description here

data <- data.frame(id_num=c(506,307,408,206), Event_1=c(1,3,5,7), Event_2=c(2,4,6,8))

enter image description here

required <- data.frame(id_num=c(206,307,408,506), Event_1=c(7,3,5,1), Event_2=c(8,4,6,2))

enter image description here

Upvotes: 1

Views: 46

Answers (1)

mindlessgreen
mindlessgreen

Reputation: 12112

merged <- merge(reference[,"id_num",drop=FALSE],data,by="id_num")

Upvotes: 2

Related Questions