Last active
October 15, 2015 14:36
-
-
Save explodecomputer/a1aa79f538fe5d64bd9b to your computer and use it in GitHub Desktop.
match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Some example data | |
# Data frame with an id column | |
data1 <- data.frame(ids = c("A", "B", "C"), value = 1:3) | |
# matrix with rownames for ids | |
data2 <- matrix(1:20, 4, 5) | |
rownames(data2) <- c("C", "D", "B", "E") | |
# Get data1 into the same order as data2 | |
data1[match(rownames(data2), data1$ids), ] | |
# Or data2 into the same order as data1 | |
data2[match(data1$ids, rownames(data2)), ] | |
# This kind of works, in that it makes the rows in data1 match data2 where it can | |
# But we get NAs in each because some ids in data1 aren't in data2 and vice versa | |
# We can extract the rows from data1 and data2 that only have ids in common | |
data1_update <- subset(data1, ids %in% rownames(data2)) | |
data2_update <- data2[rownames(data2) %in% data1$ids, ] | |
# Now we can try using match again | |
# Get data1_update into the same order as data2_update | |
data1_update[match(rownames(data2_update), data1_update$ids), ] | |
# Or data2_update into the same order as data1_update | |
data2_update[match(data1_update$ids, rownames(data2_update)), ] | |
# So ultimately we want to have two dataframes | |
# They have the same number of rows | |
# And the ids all match | |
data1 <- data1_update[match(rownames(data2_update), data1_update$ids), ] | |
data2 <- data2_update | |
save(data1, data2, file="some_file.RData") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment