Reputation: 1112
I have two data frames:
d1=read.table(text="X1 X2 X3 X4 X5
1212 1213 1214 1216 1215
1194 1195 1193 1212 1190
1555 1554 1553 1556 1557
1212 1213 1214 1216 1215
1194 1195 1193 1212 1190
1212 1213 1214 1216 1215
904 1321 1322 1323 1324
1212 1213 1214 1216 1215
1206 1207 1208 1209 1210
1206 1207 1208 1209 1210", header=T, stringsAsFactors=F)
d2=read.table(text="X1 X2 X3 X4 X5 LG
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr14 Chr12 Chr15 Chr10 Chr15 Chr15
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr08 Chr12 Chr12 Chr12 Chr12 Chr12
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11", header=T, stringsAsFactors=F)
I would like to extract value from d1 based on the evaluation: if the d2$LG equals to values from column X1 to X5. extract the values in d1 where the corresponding position in d2 is the first position having the same value as d2$LG. The extracted values is the column "PO". The expected result as below:
X1 X2 X3 X4 X5 LG PO
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1212
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1194
Chr14 Chr12 Chr15 Chr10 Chr15 Chr15 1553
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1212
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1194
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1212
Chr08 Chr12 Chr12 Chr12 Chr12 Chr12 1321
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1212
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1206
Chr11 Chr11 Chr11 Chr11 Chr11 Chr11 1206
Thank you for any helps.
Upvotes: 1
Views: 25
Reputation: 887118
We can use row/column
indexing for this. Create the column index by comparing the first 5 columns ('X1' to 'X5') with 'LG' column of 'd2', get the column index of the first match for each row with max.col
, cbind
with the row index (seq_len(nrow(d2))
) and use that to extract the values that corresponds to 'd1' (assuming both 'd1' and 'd2' - have the same dimensions)
d2$PO <- d1[cbind(seq_len(nrow(d2)), max.col(d2[1:5] == d2$LG, 'first'))]
d2$PO
#[1] 1212 1194 1553 1212 1194 1212 1321 1212 1206 1206
Upvotes: 2