Use dplyr (I think) to manipulate a datasetdata.table vs dplyr: can one do something well the other can't or does poorly?R boxplot() and summary() of frequency tableGrouping a dataset into time intervals then counting many columns within each perioddplyr left_join with timeline and datesHow to force all treatments to have same intercept in lme4?R: Using ggplot2 to plot % histogram not working for alphanumeric and conversion of freq to freq%How do I Merge two dfs over multiple keys/ emails?Iterate through columns and row values (list) in R dplyrHow can I add a column with the mutate function from dplyr in this data frame to calculate the difference in weight compared to day 0?Calculating percentages when values are weighted

Pre-Employment Background Check With Consent For Future Checks

Why didn’t Eve recognize the little cockroach as a living organism?

Is there any common country to visit for persons holding UK and Schengen visas?

What is the purpose of using a decision tree?

Reasons for having MCU pin-states default to pull-up/down out of reset

What (if any) is the reason to buy in small local stores?

Started in 1987 vs. Starting in 1987

Do people actually use the word "kaputt" in conversation?

How do I prevent inappropriate ads from appearing in my game?

"Marked down as someone wanting to sell shares." What does that mean?

A seasonal riddle

Relations between homogeneous polynomials

Recursively move files within sub directories

How can a new country break out from a developed country without war?

What should be the ideal length of sentences in a blog post for ease of reading?

What is the tangent at a sharp point on a curve?

How to track Account Description field changes in Field history Tracking?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Air travel with refrigerated insulin

Asserting that Atheism and Theism are both faith based positions

Would a primitive species be able to learn English from reading books alone?

PTIJ: Which Dr. Seuss books should one obtain?

Does capillary rise violate hydrostatic paradox?

Trouble reading roman numeral notation with flats



Use dplyr (I think) to manipulate a dataset


data.table vs dplyr: can one do something well the other can't or does poorly?R boxplot() and summary() of frequency tableGrouping a dataset into time intervals then counting many columns within each perioddplyr left_join with timeline and datesHow to force all treatments to have same intercept in lme4?R: Using ggplot2 to plot % histogram not working for alphanumeric and conversion of freq to freq%How do I Merge two dfs over multiple keys/ emails?Iterate through columns and row values (list) in R dplyrHow can I add a column with the mutate function from dplyr in this data frame to calculate the difference in weight compared to day 0?Calculating percentages when values are weighted













-1















I am giving a data set called ChickWeight. This has the weights of chicks over a time period. I need to introduce a new variable that measures the current weight difference compared to day 0.



I first cleaned the data set and took out only the chicks that were recorded for all 12 weigh ins:



library(datasets)
library(dplyr)
Frequency <- dplyr::count(ChickWeight$Chick)
colnames(Frequency)[colnames(Frequency)=="x"] <- "Chick"
a <- inner_join(ChickWeight, Frequency, by='Chick')
complete <- a[(a$freq == 12),]
head(complete,3)


This data set is in the library(datasets) of r, called ChickWeight.










share|improve this question
























  • Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

    – www
    Mar 7 at 1:33











  • I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

    – Sam Cole
    Mar 7 at 1:35











  • There is no need to call library(datasets). Also are you sure this code works?

    – NelsonGon
    Mar 7 at 1:39






  • 1





    count(ChickWeight$Chick) gave me an error.

    – www
    Mar 7 at 1:39











  • Yea it worked for me

    – Sam Cole
    Mar 7 at 1:44















-1















I am giving a data set called ChickWeight. This has the weights of chicks over a time period. I need to introduce a new variable that measures the current weight difference compared to day 0.



I first cleaned the data set and took out only the chicks that were recorded for all 12 weigh ins:



library(datasets)
library(dplyr)
Frequency <- dplyr::count(ChickWeight$Chick)
colnames(Frequency)[colnames(Frequency)=="x"] <- "Chick"
a <- inner_join(ChickWeight, Frequency, by='Chick')
complete <- a[(a$freq == 12),]
head(complete,3)


This data set is in the library(datasets) of r, called ChickWeight.










share|improve this question
























  • Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

    – www
    Mar 7 at 1:33











  • I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

    – Sam Cole
    Mar 7 at 1:35











  • There is no need to call library(datasets). Also are you sure this code works?

    – NelsonGon
    Mar 7 at 1:39






  • 1





    count(ChickWeight$Chick) gave me an error.

    – www
    Mar 7 at 1:39











  • Yea it worked for me

    – Sam Cole
    Mar 7 at 1:44













-1












-1








-1








I am giving a data set called ChickWeight. This has the weights of chicks over a time period. I need to introduce a new variable that measures the current weight difference compared to day 0.



I first cleaned the data set and took out only the chicks that were recorded for all 12 weigh ins:



library(datasets)
library(dplyr)
Frequency <- dplyr::count(ChickWeight$Chick)
colnames(Frequency)[colnames(Frequency)=="x"] <- "Chick"
a <- inner_join(ChickWeight, Frequency, by='Chick')
complete <- a[(a$freq == 12),]
head(complete,3)


This data set is in the library(datasets) of r, called ChickWeight.










share|improve this question
















I am giving a data set called ChickWeight. This has the weights of chicks over a time period. I need to introduce a new variable that measures the current weight difference compared to day 0.



I first cleaned the data set and took out only the chicks that were recorded for all 12 weigh ins:



library(datasets)
library(dplyr)
Frequency <- dplyr::count(ChickWeight$Chick)
colnames(Frequency)[colnames(Frequency)=="x"] <- "Chick"
a <- inner_join(ChickWeight, Frequency, by='Chick')
complete <- a[(a$freq == 12),]
head(complete,3)


This data set is in the library(datasets) of r, called ChickWeight.







r dplyr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 1:47







Sam Cole

















asked Mar 7 at 1:29









Sam ColeSam Cole

104




104












  • Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

    – www
    Mar 7 at 1:33











  • I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

    – Sam Cole
    Mar 7 at 1:35











  • There is no need to call library(datasets). Also are you sure this code works?

    – NelsonGon
    Mar 7 at 1:39






  • 1





    count(ChickWeight$Chick) gave me an error.

    – www
    Mar 7 at 1:39











  • Yea it worked for me

    – Sam Cole
    Mar 7 at 1:44

















  • Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

    – www
    Mar 7 at 1:33











  • I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

    – Sam Cole
    Mar 7 at 1:35











  • There is no need to call library(datasets). Also are you sure this code works?

    – NelsonGon
    Mar 7 at 1:39






  • 1





    count(ChickWeight$Chick) gave me an error.

    – www
    Mar 7 at 1:39











  • Yea it worked for me

    – Sam Cole
    Mar 7 at 1:44
















Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

– www
Mar 7 at 1:33





Are you using the count function from the dplyr package? The first argument of dplyr::count is a data frame, so I cannot run you code.

– www
Mar 7 at 1:33













I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

– Sam Cole
Mar 7 at 1:35





I am using the count function of dyplr, the ChickWeight can be found In the library(datasets)

– Sam Cole
Mar 7 at 1:35













There is no need to call library(datasets). Also are you sure this code works?

– NelsonGon
Mar 7 at 1:39





There is no need to call library(datasets). Also are you sure this code works?

– NelsonGon
Mar 7 at 1:39




1




1





count(ChickWeight$Chick) gave me an error.

– www
Mar 7 at 1:39





count(ChickWeight$Chick) gave me an error.

– www
Mar 7 at 1:39













Yea it worked for me

– Sam Cole
Mar 7 at 1:44





Yea it worked for me

– Sam Cole
Mar 7 at 1:44












1 Answer
1






active

oldest

votes


















0














You can try:



library(dplyr)

ChickWeight %>%
group_by(Chick) %>%
filter(any(Time == 21)) %>%
mutate(wdiff = weight - first(weight))

# A tibble: 540 x 5
# Groups: Chick [45]
weight Time Chick Diet wdiff
<dbl> <dbl> <ord> <fct> <dbl>
1 42 0 1 1 0
2 51 2 1 1 9
3 59 4 1 1 17
4 64 6 1 1 22
5 76 8 1 1 34
6 93 10 1 1 51
7 106 12 1 1 64
8 125 14 1 1 83
9 149 16 1 1 107
10 171 18 1 1 129
# ... with 530 more rows





share|improve this answer























  • So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

    – Sam Cole
    Mar 7 at 5:07












  • That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

    – H 1
    Mar 7 at 5:44










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034698%2fuse-dplyr-i-think-to-manipulate-a-dataset%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









0














You can try:



library(dplyr)

ChickWeight %>%
group_by(Chick) %>%
filter(any(Time == 21)) %>%
mutate(wdiff = weight - first(weight))

# A tibble: 540 x 5
# Groups: Chick [45]
weight Time Chick Diet wdiff
<dbl> <dbl> <ord> <fct> <dbl>
1 42 0 1 1 0
2 51 2 1 1 9
3 59 4 1 1 17
4 64 6 1 1 22
5 76 8 1 1 34
6 93 10 1 1 51
7 106 12 1 1 64
8 125 14 1 1 83
9 149 16 1 1 107
10 171 18 1 1 129
# ... with 530 more rows





share|improve this answer























  • So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

    – Sam Cole
    Mar 7 at 5:07












  • That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

    – H 1
    Mar 7 at 5:44















0














You can try:



library(dplyr)

ChickWeight %>%
group_by(Chick) %>%
filter(any(Time == 21)) %>%
mutate(wdiff = weight - first(weight))

# A tibble: 540 x 5
# Groups: Chick [45]
weight Time Chick Diet wdiff
<dbl> <dbl> <ord> <fct> <dbl>
1 42 0 1 1 0
2 51 2 1 1 9
3 59 4 1 1 17
4 64 6 1 1 22
5 76 8 1 1 34
6 93 10 1 1 51
7 106 12 1 1 64
8 125 14 1 1 83
9 149 16 1 1 107
10 171 18 1 1 129
# ... with 530 more rows





share|improve this answer























  • So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

    – Sam Cole
    Mar 7 at 5:07












  • That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

    – H 1
    Mar 7 at 5:44













0












0








0







You can try:



library(dplyr)

ChickWeight %>%
group_by(Chick) %>%
filter(any(Time == 21)) %>%
mutate(wdiff = weight - first(weight))

# A tibble: 540 x 5
# Groups: Chick [45]
weight Time Chick Diet wdiff
<dbl> <dbl> <ord> <fct> <dbl>
1 42 0 1 1 0
2 51 2 1 1 9
3 59 4 1 1 17
4 64 6 1 1 22
5 76 8 1 1 34
6 93 10 1 1 51
7 106 12 1 1 64
8 125 14 1 1 83
9 149 16 1 1 107
10 171 18 1 1 129
# ... with 530 more rows





share|improve this answer













You can try:



library(dplyr)

ChickWeight %>%
group_by(Chick) %>%
filter(any(Time == 21)) %>%
mutate(wdiff = weight - first(weight))

# A tibble: 540 x 5
# Groups: Chick [45]
weight Time Chick Diet wdiff
<dbl> <dbl> <ord> <fct> <dbl>
1 42 0 1 1 0
2 51 2 1 1 9
3 59 4 1 1 17
4 64 6 1 1 22
5 76 8 1 1 34
6 93 10 1 1 51
7 106 12 1 1 64
8 125 14 1 1 83
9 149 16 1 1 107
10 171 18 1 1 129
# ... with 530 more rows






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 1:41









H 1H 1

3,07711124




3,07711124












  • So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

    – Sam Cole
    Mar 7 at 5:07












  • That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

    – H 1
    Mar 7 at 5:44

















  • So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

    – Sam Cole
    Mar 7 at 5:07












  • That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

    – H 1
    Mar 7 at 5:44
















So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

– Sam Cole
Mar 7 at 5:07






So, is there a way to make it so that the weight - first(weight) is weight - first(weight of each chick at time 0). As of now this just subtracts all the weights by 42 which is the wight of chick 1 at time 0

– Sam Cole
Mar 7 at 5:07














That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

– H 1
Mar 7 at 5:44





That's what it's doing. Did you run the code above in its entirety? You need to ensure that the data frame is still grouped.

– H 1
Mar 7 at 5:44



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034698%2fuse-dplyr-i-think-to-manipulate-a-dataset%23new-answer', 'question_page');

);

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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко