user1615411
user1615411

Reputation: 11

Choose part of Row Names as qualifier

I'm currently trying to select rows from a data frame in R based on the row names of another data frame, which is usually straight forward:

data1 <- data1[which(row.names(data1) %in% row.names(data2))]

But, my current challenge involves differing lengths of row names between the two data sets. My first data frame has row names that are the first 12 characters of the row names of my second data frame. I want to be able to pick out the rows that have the first 12 characters matching across the two data sets but I'm having problems. Any suggestions?

Upvotes: 1

Views: 102

Answers (1)

Adam Zalcman
Adam Zalcman

Reputation: 27233

Use substr():

data1 <- data1[which(row.names(data1) %in% substr(row.names(data2), 1, 12)), ]

Upvotes: 3

Related Questions