How to traverse two columns and fill in another column at the same index with a string in R?
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.
r dataframe
add a comment |
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.
r dataframe
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40
add a comment |
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.
r dataframe
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.
r dataframe
r dataframe
asked Nov 20 at 1:43
Griffin Weinhold
1
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40
add a comment |
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40
add a comment |
1 Answer
1
active
oldest
votes
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.
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.
– Griffin Weinhold
Nov 20 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 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385073%2fhow-to-traverse-two-columns-and-fill-in-another-column-at-the-same-index-with-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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.
– Griffin Weinhold
Nov 20 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 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
add a comment |
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.
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.
– Griffin Weinhold
Nov 20 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 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
add a comment |
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.
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.
edited Nov 21 at 1:10
answered Nov 20 at 1:52
iod
3,4892721
3,4892721
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.
– Griffin Weinhold
Nov 20 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 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
add a comment |
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.
– Griffin Weinhold
Nov 20 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 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
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.
– Griffin Weinhold
Nov 20 at 22:51
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.
– Griffin Weinhold
Nov 20 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 at 23:23
It's really hard to "imagine" this. It would really be best if you'd provided a reproducible example.
– iod
Nov 20 at 23:23
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
Also, did you try my solution? It should work for what you're describing.
– iod
Nov 21 at 1:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385073%2fhow-to-traverse-two-columns-and-fill-in-another-column-at-the-same-index-with-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 20 at 2:40