Austin
Austin

Reputation: 121

Merge by row number in R

I am trying to merge two data frames, dat1 and dat2.

dat1 is an n x 65 data frame, whose rownames correspond to specific values in dat2. Here's a smaller, toy example:

 year <- c(1998, 2001, 2002, 2004, 2008)
 make <- c("Ford", "Dodge", "Toyota", "Tesla", "Jeep") 
 model <- c("Escape", "Ram", "Camry", "Model S", "Wrangler")
 price <- c(4750.00, 14567.00, 20000.00, 60123.00, 18469.00) 
 dat1 <- data.frame(year, make, model, price)

dat2 is an nx1 vector with a single column called rownumber. It contains row numbers from dat1 which are of interest. Here's an example:

dat2 <- as.data.frame(c(12, 45, 56, 123))
colnames(dat1)[1] <- "rownumber" 

I attempt to merge both data frames as follows:

dat3 <- merge(dat1, dat2, by = "row.names", all.x = TRUE, all.y = FALSE)

The problem is that dat3 contains two columns, row.names and rownumber which do not match. For example, in row one of dat3, row.names = 1, but rownumber = 777.

Any thoughts on how to resolve this? Thanks in advance.

Upvotes: 0

Views: 16508

Answers (2)

Austin
Austin

Reputation: 121

Thanks, all for the quick responses, and especially @aashanand for the elegant solution. Here's the quick way:

dat3 <- dat1[dat2$rownumbers, ]

Upvotes: 2

aashanand
aashanand

Reputation: 754

Say you have a data.frame object called dat1 as defined below:

> year <- c(1998, 2001, 2002, 2004, 2008)
> make <- c("Ford", "Dodge", "Toyota", "Tesla", "Jeep") 
> model <- c("Escape", "Ram", "Camry", "Model S", "Wrangler")
> price <- c(4750.00, 14567.00, 20000.00, 60123.00, 18469.00) 
> dat1 <- data.frame(year, make, model, price)

Say you have a vector rownumbers that defines rows of interest from dat1.

> rownumbers <- c(2,5)

Your question does not state the desired results but assuming you want to subset rows of dat1 such that their row numbers are in the vector rownumbers, one simple way to do this is:

> dat1[rownumbers,] # Don't forget the comma
  year  make    model price
2 2001 Dodge      Ram 14567
5 2008  Jeep Wrangler 18469

Edit: you can assign your subset to a new variable dat3 if you'd like.

> dat3 <- dat1[rownumbers,]

Upvotes: 5

Related Questions