-1

How to use a dataset to extract specific columns from another dataset?

2 Answers2

0

It's always better to supply a minimal reproducible example:

df1 <- data.frame(V1 = 1:3,
                  V2 = 4:6,
                  V3 = 7:9)

df2 <- data.frame(snp = c("V2", "V3"),
                  stringsAsFactors=FALSE)

Now we can use a character vector to index the columns we want:

df1[, df2$snp]

Returns:

  V2 V3
1  4  7
2  5  8
3  6  9

Edit:

Would you know how to do this so that it retains the "i..POP" column in data2?

df1 <- data.frame(ID = letters[1:3],
                  V1 = 1:3,
                  V2 = 4:6,
                  V3 = 7:9)
names(df1)[1] <- "ï..POP"

df2 <- data.frame(snp = c("V2", "V3"),
                  stringsAsFactors=FALSE)

We can use c to combine the names of the columns:

df1[, c("ï..POP", df2$snp)]
  ï..POP V2 V3
1      a  4  7
2      b  5  8
3      c  6  9
dario
  • 6,415
  • 2
  • 12
  • 26
  • Works as long as `df2$snp` is a subset of `df1` column names. – mob Mar 06 '20 at 17:11
  • @mob Going from the question and presented data I was assuming that. But you are of course right... Anyways, that's where a MRE comes in handy. – dario Mar 06 '20 at 17:13
0

Use intersect to find common names between two data sets.

snp.common <- intersect(data1$snp, colnames(data2$snp))
data2.separated <- data2[,snp.common]
mob
  • 117,087
  • 18
  • 149
  • 283