-3

I have a data frame with a column of strings that are the question body of a survey, then I have a separate data frame with those question bodies matched two a question number. I want to traverse the original data frame's column and check if the value matches any within the other data frame and if does I want to store the associated question number in a column in the original df. I am having a lot of trouble figuring this out, I have looked into using apply() or something like that but I can't quite get it. Any help would be greatly appreciated.

1

1 Answer 1

0

If df is the first dataframe and df2 the second and Q is the name of the question strings column, then:

library (dplyr) 
left_join(df1, df2, by=question_body) %>% select(-question_body)

Of course, it would be easier to give you an accurate answer if you provided some actual examples of your data structure.

3
  • Imagine we have df1 where it is a matrix with columns (question_id, question_body, participant_id) and each column has thousands of different rows. Then there is a much smaller data frame df2 that is a matrix with columns (question_number, question_body) and has the template of the original questionnaire and its responses. Now df1 is much larger and has many repetitions of the questions in df2, but I want to replace every instance of each question_body in df1 with the corresponding question_number from df2 if that makes sense. Nov 20, 2018 at 22:51
  • It's really hard to "imagine" this. It would really be best if you'd provided a reproducible example.
    – iod
    Nov 20, 2018 at 23:23
  • Also, did you try my solution? It should work for what you're describing.
    – iod
    Nov 21, 2018 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.