jeudi 26 novembre 2015

subset values in a df based on values in another df

I have two dfs, both dim [1] 54 210. One (lets call it dfx) contains 1, 0s to mark incorrect and correct answers on a test. dfy contains the response time for each of these questions. I'd like to subset(merge() (perhaps) all items from dfy that are == 1 in dfx. The data is in the wide format, ID = rownames and columns represent each question.

Example:

dfx

Q1 Q2 Q3 Q4 Q5 …
1  1  1  1  1
1  1  1  1  1
1  1  0  1  1 
1  1  0  1  1 

Dfy

Q1_3 Q2_3  Q3_3  Q4_3  Q5_3 ...
16.01 8.23 18.13 11.14 18.03
17.25 7.50 11.72 10.84  7.24

I would need a dfz that is a subset of dfy, in which if dfx[Q1] == 1, dfy [Q1_3] is returned as dfz[Q1_3], otherwise NA or dfx[Q1]( which is 0).

I can do it if I specify cols by dfz<- cbind(ifelse(dfx$Q1 == 1, dfy$Q1_3, dfx$Q1)), however I don't know how to apply it for the whole df.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire