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.
Say you have a
data.frame
object calleddat1
as defined below:Say you have a vector
rownumbers
that defines rows of interest fromdat1
.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 vectorrownumbers
, one simple way to do this is:Edit: you can assign your subset to a new variable
dat3
if you'd like.Thanks, all for the quick responses, and especially @aashanand for the elegant solution. Here's the quick way: