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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved