How do I get count of number of items in selection?How to sort a dataframe by multiple column(s)Calculating...
How do Windows version numbers work?
Is it rude to tell recruiters I would only change jobs for a better salary?
Are there any intersection of Theory A and Theory B?
Email about missed connecting flight compensation 5 months after flight, is there a point?
When did the Roman Empire fall according to contemporaries?
Where is the USB2 OTG port on the RPi 4 Model B located?
Extract an attribute value from XML
Password maker in c#
Creating custom objects with custom properties using generics
How can I deal with a player trying to insert real-world mythology into my homebrew setting?
Referring to different instances of the same character in time travel
diff shows a file that does not exist
Optimising Table wrapping over a Select
Where or how can I find what interfaces an out of the box Apex class implements?
Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?
If a specific mass of air is polluted, will the pollution stick with it?
What's the point of this scene involving Flash Thompson at the airport?
How do I determine whether a permit is required for a new gas line?
Why would guns not work in the dungeon?
Grammy Winners Grading
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
How did the hit man miss?
Does Google Maps take into account hills/inclines for route times?
Why is dry soil hydrophobic? Bad gardener paradox
How do I get count of number of items in selection?
How to sort a dataframe by multiple column(s)Calculating the occurrences of numbers in the subsets of a data.frameHow to make a great R reproducible exampleSelecting a range of rows from R data frameSubsetting rows with logical comparisons when missing observations are presentSelecting Specific Dates in Rcounting number of observations into a dataframeR subset data.frame columns by group to maximize row valuessubset by at least two out of multiple conditionsCounting Duplicates without aggregation or group_by
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I want to list in array format how many in each Diet group (there are four) have Time > 21.
I have tried to solve this in RStudio.
data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
In order to find how many observations are in newdata, I used
nrow(newdata)
,
but I would like to find out how many observations meet the criteria just by making it a part of this expression:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
so that when I display newdata
the table will also contain the number of observations that meet the criteria in a new column.
Desire output:
Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100
Is there a way to do that?
r subset frequency
add a comment |
I want to list in array format how many in each Diet group (there are four) have Time > 21.
I have tried to solve this in RStudio.
data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
In order to find how many observations are in newdata, I used
nrow(newdata)
,
but I would like to find out how many observations meet the criteria just by making it a part of this expression:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
so that when I display newdata
the table will also contain the number of observations that meet the criteria in a new column.
Desire output:
Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100
Is there a way to do that?
r subset frequency
and the obs count would be a repeating number in a different column ofnewdata
? What aboutnewdata$obs_count <- nrow(newdata)
?
– avid_useR
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
So basically you want to get the obs count for eachDiet
group?
– avid_useR
8 hours ago
add a comment |
I want to list in array format how many in each Diet group (there are four) have Time > 21.
I have tried to solve this in RStudio.
data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
In order to find how many observations are in newdata, I used
nrow(newdata)
,
but I would like to find out how many observations meet the criteria just by making it a part of this expression:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
so that when I display newdata
the table will also contain the number of observations that meet the criteria in a new column.
Desire output:
Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100
Is there a way to do that?
r subset frequency
I want to list in array format how many in each Diet group (there are four) have Time > 21.
I have tried to solve this in RStudio.
data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
In order to find how many observations are in newdata, I used
nrow(newdata)
,
but I would like to find out how many observations meet the criteria just by making it a part of this expression:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
so that when I display newdata
the table will also contain the number of observations that meet the criteria in a new column.
Desire output:
Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100
Is there a way to do that?
r subset frequency
r subset frequency
edited 8 hours ago
M-M
10.3k6 gold badges26 silver badges49 bronze badges
10.3k6 gold badges26 silver badges49 bronze badges
asked 9 hours ago
MetsfanMetsfan
414 bronze badges
414 bronze badges
and the obs count would be a repeating number in a different column ofnewdata
? What aboutnewdata$obs_count <- nrow(newdata)
?
– avid_useR
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
So basically you want to get the obs count for eachDiet
group?
– avid_useR
8 hours ago
add a comment |
and the obs count would be a repeating number in a different column ofnewdata
? What aboutnewdata$obs_count <- nrow(newdata)
?
– avid_useR
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
So basically you want to get the obs count for eachDiet
group?
– avid_useR
8 hours ago
and the obs count would be a repeating number in a different column of
newdata
? What about newdata$obs_count <- nrow(newdata)
?– avid_useR
9 hours ago
and the obs count would be a repeating number in a different column of
newdata
? What about newdata$obs_count <- nrow(newdata)
?– avid_useR
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
So basically you want to get the obs count for each
Diet
group?– avid_useR
8 hours ago
So basically you want to get the obs count for each
Diet
group?– avid_useR
8 hours ago
add a comment |
4 Answers
4
active
oldest
votes
It can be done in base
:
transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))
#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code withouttransform
to see). You should runtable
on couple more dataframes that you have to know what it does better.
– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
|
show 1 more comment
Consider a straightforward aggregate
after the subset
call:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)
# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9
add a comment |
We can do this with summarize
from dplyr
:
library(dplyr)
newdata %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
We can even combine the subset
to a single dplyr
workflow:
ChickWeight %>%
filter(Time >= 21) %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
Output:
# A tibble: 4 x 2
Diet Num_Obs
<fct> <int>
1 1 16
2 2 10
3 3 10
4 4 9
add a comment |
Here is a data table approach
library(data.table)
df <- as.data.table(ChickWeight)
df[Time >= 21, .(Number = .N), by = Diet]
# Diet Number
# 1: 1 16
# 2: 2 10
# 3: 3 10
# 4: 4 9
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%2f57011287%2fhow-do-i-get-count-of-number-of-items-in-selection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It can be done in base
:
transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))
#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code withouttransform
to see). You should runtable
on couple more dataframes that you have to know what it does better.
– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
|
show 1 more comment
It can be done in base
:
transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))
#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code withouttransform
to see). You should runtable
on couple more dataframes that you have to know what it does better.
– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
|
show 1 more comment
It can be done in base
:
transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))
#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9
It can be done in base
:
transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))
#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9
answered 8 hours ago
M-MM-M
10.3k6 gold badges26 silver badges49 bronze badges
10.3k6 gold badges26 silver badges49 bronze badges
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code withouttransform
to see). You should runtable
on couple more dataframes that you have to know what it does better.
– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
|
show 1 more comment
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code withouttransform
to see). You should runtable
on couple more dataframes that you have to know what it does better.
– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
M-M, thanks. It works. What is the purpose of "table"? Why is it needed?
– Metsfan
8 hours ago
@Metsfan You can read about it by running
?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform
to see). You should run table
on couple more dataframes that you have to know what it does better.– M-M
8 hours ago
@Metsfan You can read about it by running
?table()
. In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform
to see). You should run table
on couple more dataframes that you have to know what it does better.– M-M
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"
– Metsfan
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:
table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
@Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after:
table(Diet=subset(ChickWeight, Time >= 21, select=Diet))
– M-M
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.
– Metsfan
8 hours ago
|
show 1 more comment
Consider a straightforward aggregate
after the subset
call:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)
# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9
add a comment |
Consider a straightforward aggregate
after the subset
call:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)
# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9
add a comment |
Consider a straightforward aggregate
after the subset
call:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)
# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9
Consider a straightforward aggregate
after the subset
call:
newdata <- subset(ChickWeight, Time >= 21, select=Diet)
aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)
# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9
answered 8 hours ago
ParfaitParfait
58.5k10 gold badges55 silver badges75 bronze badges
58.5k10 gold badges55 silver badges75 bronze badges
add a comment |
add a comment |
We can do this with summarize
from dplyr
:
library(dplyr)
newdata %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
We can even combine the subset
to a single dplyr
workflow:
ChickWeight %>%
filter(Time >= 21) %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
Output:
# A tibble: 4 x 2
Diet Num_Obs
<fct> <int>
1 1 16
2 2 10
3 3 10
4 4 9
add a comment |
We can do this with summarize
from dplyr
:
library(dplyr)
newdata %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
We can even combine the subset
to a single dplyr
workflow:
ChickWeight %>%
filter(Time >= 21) %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
Output:
# A tibble: 4 x 2
Diet Num_Obs
<fct> <int>
1 1 16
2 2 10
3 3 10
4 4 9
add a comment |
We can do this with summarize
from dplyr
:
library(dplyr)
newdata %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
We can even combine the subset
to a single dplyr
workflow:
ChickWeight %>%
filter(Time >= 21) %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
Output:
# A tibble: 4 x 2
Diet Num_Obs
<fct> <int>
1 1 16
2 2 10
3 3 10
4 4 9
We can do this with summarize
from dplyr
:
library(dplyr)
newdata %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
We can even combine the subset
to a single dplyr
workflow:
ChickWeight %>%
filter(Time >= 21) %>%
group_by(Diet) %>%
summarize(Num_Obs = n())
Output:
# A tibble: 4 x 2
Diet Num_Obs
<fct> <int>
1 1 16
2 2 10
3 3 10
4 4 9
edited 6 hours ago
answered 8 hours ago
avid_useRavid_useR
14.1k4 gold badges20 silver badges33 bronze badges
14.1k4 gold badges20 silver badges33 bronze badges
add a comment |
add a comment |
Here is a data table approach
library(data.table)
df <- as.data.table(ChickWeight)
df[Time >= 21, .(Number = .N), by = Diet]
# Diet Number
# 1: 1 16
# 2: 2 10
# 3: 3 10
# 4: 4 9
add a comment |
Here is a data table approach
library(data.table)
df <- as.data.table(ChickWeight)
df[Time >= 21, .(Number = .N), by = Diet]
# Diet Number
# 1: 1 16
# 2: 2 10
# 3: 3 10
# 4: 4 9
add a comment |
Here is a data table approach
library(data.table)
df <- as.data.table(ChickWeight)
df[Time >= 21, .(Number = .N), by = Diet]
# Diet Number
# 1: 1 16
# 2: 2 10
# 3: 3 10
# 4: 4 9
Here is a data table approach
library(data.table)
df <- as.data.table(ChickWeight)
df[Time >= 21, .(Number = .N), by = Diet]
# Diet Number
# 1: 1 16
# 2: 2 10
# 3: 3 10
# 4: 4 9
answered 6 hours ago
IceCreamToucanIceCreamToucan
13.1k1 gold badge8 silver badges19 bronze badges
13.1k1 gold badge8 silver badges19 bronze badges
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%2f57011287%2fhow-do-i-get-count-of-number-of-items-in-selection%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
and the obs count would be a repeating number in a different column of
newdata
? What aboutnewdata$obs_count <- nrow(newdata)
?– avid_useR
9 hours ago
I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.
– Metsfan
9 hours ago
Please post your desired output in the question body itself
– avid_useR
9 hours ago
So basically you want to get the obs count for each
Diet
group?– avid_useR
8 hours ago