Creating a variable based on a word count within a variable
I have a data set containing countries and their constitutions. I was wondering if there was a way to create a variable to show how many times the word "god" shows in the variable of constitutions.
The data set looks as following:
Country Year Preamble
Afghanistan 2004 In the name of Allah...
Albania 1998 We, the people of Albania...
... .... .......
and so on and so forth. I am particularly interested in knowing if there is a function in which can count how many times a specific word is used within a categorical variable or if there is a better way to accomplish what I am trying to do.
r
add a comment |
I have a data set containing countries and their constitutions. I was wondering if there was a way to create a variable to show how many times the word "god" shows in the variable of constitutions.
The data set looks as following:
Country Year Preamble
Afghanistan 2004 In the name of Allah...
Albania 1998 We, the people of Albania...
... .... .......
and so on and so forth. I am particularly interested in knowing if there is a function in which can count how many times a specific word is used within a categorical variable or if there is a better way to accomplish what I am trying to do.
r
Welcome to SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
What you need to do is make your question reproducible. In particular, share an example of your data usingdput(your_df)
. Please edit the question with this info (don't post the data in comments)
– Conor Neilson
Nov 25 '18 at 21:24
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37
add a comment |
I have a data set containing countries and their constitutions. I was wondering if there was a way to create a variable to show how many times the word "god" shows in the variable of constitutions.
The data set looks as following:
Country Year Preamble
Afghanistan 2004 In the name of Allah...
Albania 1998 We, the people of Albania...
... .... .......
and so on and so forth. I am particularly interested in knowing if there is a function in which can count how many times a specific word is used within a categorical variable or if there is a better way to accomplish what I am trying to do.
r
I have a data set containing countries and their constitutions. I was wondering if there was a way to create a variable to show how many times the word "god" shows in the variable of constitutions.
The data set looks as following:
Country Year Preamble
Afghanistan 2004 In the name of Allah...
Albania 1998 We, the people of Albania...
... .... .......
and so on and so forth. I am particularly interested in knowing if there is a function in which can count how many times a specific word is used within a categorical variable or if there is a better way to accomplish what I am trying to do.
r
r
edited Nov 25 '18 at 23:34
isp-zax
2,201511
2,201511
asked Nov 25 '18 at 20:49
Ian ChamberlinIan Chamberlin
11
11
Welcome to SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
What you need to do is make your question reproducible. In particular, share an example of your data usingdput(your_df)
. Please edit the question with this info (don't post the data in comments)
– Conor Neilson
Nov 25 '18 at 21:24
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37
add a comment |
Welcome to SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
What you need to do is make your question reproducible. In particular, share an example of your data usingdput(your_df)
. Please edit the question with this info (don't post the data in comments)
– Conor Neilson
Nov 25 '18 at 21:24
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37
Welcome to SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Welcome to SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
What you need to do is make your question reproducible. In particular, share an example of your data using
dput(your_df)
. Please edit the question with this info (don't post the data in comments)– Conor Neilson
Nov 25 '18 at 21:24
What you need to do is make your question reproducible. In particular, share an example of your data using
dput(your_df)
. Please edit the question with this info (don't post the data in comments)– Conor Neilson
Nov 25 '18 at 21:24
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37
add a comment |
1 Answer
1
active
oldest
votes
Say you want to count the number of times 'Al' appears in the above dataset, you can use grep like this:
For only one column:
grep("Al", data$Preamble)
For all columns:
lapply(data, function(x) grep("Al", x))
$`Country`
[1] 2
$Year
integer(0)
$Preamble
[1] 1 2
This will tell you in which rows and columns the match is found, ie one in the 'Country' column and two in the 'Preamble' column
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%2f53471826%2fcreating-a-variable-based-on-a-word-count-within-a-variable%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
Say you want to count the number of times 'Al' appears in the above dataset, you can use grep like this:
For only one column:
grep("Al", data$Preamble)
For all columns:
lapply(data, function(x) grep("Al", x))
$`Country`
[1] 2
$Year
integer(0)
$Preamble
[1] 1 2
This will tell you in which rows and columns the match is found, ie one in the 'Country' column and two in the 'Preamble' column
add a comment |
Say you want to count the number of times 'Al' appears in the above dataset, you can use grep like this:
For only one column:
grep("Al", data$Preamble)
For all columns:
lapply(data, function(x) grep("Al", x))
$`Country`
[1] 2
$Year
integer(0)
$Preamble
[1] 1 2
This will tell you in which rows and columns the match is found, ie one in the 'Country' column and two in the 'Preamble' column
add a comment |
Say you want to count the number of times 'Al' appears in the above dataset, you can use grep like this:
For only one column:
grep("Al", data$Preamble)
For all columns:
lapply(data, function(x) grep("Al", x))
$`Country`
[1] 2
$Year
integer(0)
$Preamble
[1] 1 2
This will tell you in which rows and columns the match is found, ie one in the 'Country' column and two in the 'Preamble' column
Say you want to count the number of times 'Al' appears in the above dataset, you can use grep like this:
For only one column:
grep("Al", data$Preamble)
For all columns:
lapply(data, function(x) grep("Al", x))
$`Country`
[1] 2
$Year
integer(0)
$Preamble
[1] 1 2
This will tell you in which rows and columns the match is found, ie one in the 'Country' column and two in the 'Preamble' column
edited Nov 25 '18 at 22:32
answered Nov 25 '18 at 21:48
RABRAB
1,410317
1,410317
add a comment |
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.
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%2f53471826%2fcreating-a-variable-based-on-a-word-count-within-a-variable%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 SO. Click on the "r" below your question. Go to the "info" tab and read up on some expectations for how to specify questions including sample data. That should help you help others to help you. Further, this is a pretty broad question and you've kind of asked 2 questions in one, so you might want to consider paring it down a bit and include said sample data (or link to a sample on a reputable file sharing site or GitHub/GitLab).
– hrbrmstr
Nov 25 '18 at 21:09
Sorry, I really try to make it clear. I am very new to coding and have a hard time describing what I am trying to accomplish. Do you have any advice as to how I could make it more specific?
– Ian Chamberlin
Nov 25 '18 at 21:21
What you need to do is make your question reproducible. In particular, share an example of your data using
dput(your_df)
. Please edit the question with this info (don't post the data in comments)– Conor Neilson
Nov 25 '18 at 21:24
@IanChamberlin I'll make it more direct. The process I asked you to follow goes to here: stackoverflow.com/tags/r/info. One of those links is what Conor provided. Please do some fundamental reading to learn how to make it easier for folks to help you.
– hrbrmstr
Nov 25 '18 at 21:37