How to add the results of applying a function to an existing data frame?Adding a horizontal line to a scatterplot in ggplot2Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Grouping functions (tapply, by, aggregate) and the *apply familyR - list to data frameDrop data frame columns by nameChanging column names of a data frameHow could I find the growth rate of GDP in RR Code for transforming integers to dateHow to construct a table of changes using data.table in R?Interpolation to estimate the middle years data in R

Cisco 3750X Power Cable

How do you earn the reader's trust?

Ribbon Cable Cross Talk - Is there a fix after the fact?

Who wrote “A writer only begins a book. A reader finishes it.”

Are runways booked by airlines to land their planes?

Are PMR446 walkie-talkies legal in Switzerland?

EU rights when flight delayed so much that return is missed

Why is std::ssize() introduced in C++20

Handling decimals in somewhat complex math

Can attacking players use activated abilities after blockers have been declared?

Python script to extract text from PDF with images

ifconfig shows UP while ip link shows DOWN

How to create a `range`-like iterable object of floats?

How did the Allies achieve air superiority on Sicily?

Can a UK national work as a paid shop assistant in the USA?

How would a developer who mostly fixed bugs for years at a company call out their contributions in their CV?

Prince of Darkness goes cryptic

Are there any German nonsense poems (Jabberwocky)?

What is to the west of Westeros?

Did Game of Thrones end the way that George RR Martin intended?

Is superuser the same as root?

How does the Earth's center produce heat?

What could be my risk mitigation strategies if my client wants to contract UAT?

Why the function ScalingFunctions does not work?



How to add the results of applying a function to an existing data frame?


Adding a horizontal line to a scatterplot in ggplot2Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Grouping functions (tapply, by, aggregate) and the *apply familyR - list to data frameDrop data frame columns by nameChanging column names of a data frameHow could I find the growth rate of GDP in RR Code for transforming integers to dateHow to construct a table of changes using data.table in R?Interpolation to estimate the middle years data in R






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to calculate the confidence intervals of some rates.
I am using tidyverse and epitools to calculate CI from Byar's method.



I am almost certainly doing something wrong.



library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


# this is a simplification, I have the population values in another table, which I've merged
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
ifelse(DATA$AREA == "B",3,
ifelse(DATA$AREA == "C",7,0)))


# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA)


Then if I want to calculate CI I thought this would work



rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA) %>%
mutate(pois.byar(rates$n,rates$POPN))


but i get



Error in mutate_impl(.data, dots) : 
Evaluation error: arguments imply differing number of rows: 0, 1.


This however works:



pois.byar(rates$n,rates$POPN)


It seems daft to then say: "turn the results of the pois.byar function into a dataframe and then merge back to the original". I may have tried that just to get some data.... I don't want to do that. It's not the right way to do things.



Any advice gratefully received.
I think it's a fairly basic problem. And indicative of my not sitting and learning but trying to do things as I go.



Here's what I want
Disease Year n area popn x pt rate lower upper conf.level










share|improve this question
























  • pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

    – Maurits Evers
    Mar 23 at 22:20











  • At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

    – damo
    Mar 23 at 22:22











  • I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

    – damo
    Mar 23 at 22:26











  • pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

    – Maurits Evers
    Mar 23 at 22:30


















1















I am trying to calculate the confidence intervals of some rates.
I am using tidyverse and epitools to calculate CI from Byar's method.



I am almost certainly doing something wrong.



library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


# this is a simplification, I have the population values in another table, which I've merged
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
ifelse(DATA$AREA == "B",3,
ifelse(DATA$AREA == "C",7,0)))


# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA)


Then if I want to calculate CI I thought this would work



rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA) %>%
mutate(pois.byar(rates$n,rates$POPN))


but i get



Error in mutate_impl(.data, dots) : 
Evaluation error: arguments imply differing number of rows: 0, 1.


This however works:



pois.byar(rates$n,rates$POPN)


It seems daft to then say: "turn the results of the pois.byar function into a dataframe and then merge back to the original". I may have tried that just to get some data.... I don't want to do that. It's not the right way to do things.



Any advice gratefully received.
I think it's a fairly basic problem. And indicative of my not sitting and learning but trying to do things as I go.



Here's what I want
Disease Year n area popn x pt rate lower upper conf.level










share|improve this question
























  • pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

    – Maurits Evers
    Mar 23 at 22:20











  • At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

    – damo
    Mar 23 at 22:22











  • I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

    – damo
    Mar 23 at 22:26











  • pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

    – Maurits Evers
    Mar 23 at 22:30














1












1








1








I am trying to calculate the confidence intervals of some rates.
I am using tidyverse and epitools to calculate CI from Byar's method.



I am almost certainly doing something wrong.



library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


# this is a simplification, I have the population values in another table, which I've merged
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
ifelse(DATA$AREA == "B",3,
ifelse(DATA$AREA == "C",7,0)))


# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA)


Then if I want to calculate CI I thought this would work



rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA) %>%
mutate(pois.byar(rates$n,rates$POPN))


but i get



Error in mutate_impl(.data, dots) : 
Evaluation error: arguments imply differing number of rows: 0, 1.


This however works:



pois.byar(rates$n,rates$POPN)


It seems daft to then say: "turn the results of the pois.byar function into a dataframe and then merge back to the original". I may have tried that just to get some data.... I don't want to do that. It's not the right way to do things.



Any advice gratefully received.
I think it's a fairly basic problem. And indicative of my not sitting and learning but trying to do things as I go.



Here's what I want
Disease Year n area popn x pt rate lower upper conf.level










share|improve this question
















I am trying to calculate the confidence intervals of some rates.
I am using tidyverse and epitools to calculate CI from Byar's method.



I am almost certainly doing something wrong.



library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


# this is a simplification, I have the population values in another table, which I've merged
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
ifelse(DATA$AREA == "B",3,
ifelse(DATA$AREA == "C",7,0)))


# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA)


Then if I want to calculate CI I thought this would work



rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA) %>%
mutate(pois.byar(rates$n,rates$POPN))


but i get



Error in mutate_impl(.data, dots) : 
Evaluation error: arguments imply differing number of rows: 0, 1.


This however works:



pois.byar(rates$n,rates$POPN)


It seems daft to then say: "turn the results of the pois.byar function into a dataframe and then merge back to the original". I may have tried that just to get some data.... I don't want to do that. It's not the right way to do things.



Any advice gratefully received.
I think it's a fairly basic problem. And indicative of my not sitting and learning but trying to do things as I go.



Here's what I want
Disease Year n area popn x pt rate lower upper conf.level







r tidyverse confidence-interval mutate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 22:41







damo

















asked Mar 23 at 21:48









damodamo

678




678












  • pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

    – Maurits Evers
    Mar 23 at 22:20











  • At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

    – damo
    Mar 23 at 22:22











  • I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

    – damo
    Mar 23 at 22:26











  • pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

    – Maurits Evers
    Mar 23 at 22:30


















  • pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

    – Maurits Evers
    Mar 23 at 22:20











  • At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

    – damo
    Mar 23 at 22:22











  • I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

    – damo
    Mar 23 at 22:26











  • pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

    – Maurits Evers
    Mar 23 at 22:30

















pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

– Maurits Evers
Mar 23 at 22:20





pois.byvar returns a data.frame. How do expect to "integrate" that into your existing data.frame? Can you show your expected output?

– Maurits Evers
Mar 23 at 22:20













At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

– damo
Mar 23 at 22:22





At the moment I have a row for Marco polio for area A. I'd like a data frame with the n, the popn, and then the output of pois.byar on the same row.

– damo
Mar 23 at 22:22













I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

– damo
Mar 23 at 22:26





I'd have thought the pipe and mutate to add pois.byar would do this. But it seems not to.

– damo
Mar 23 at 22:26













pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

– Maurits Evers
Mar 23 at 22:30






pois.byar returns a data.frame so mutate will not accept that unless you wrap it in a list. See my answer below.

– Maurits Evers
Mar 23 at 22:30













1 Answer
1






active

oldest

votes


















1














It's not clear to me what your expected output is supposed to me. Your comment does not really help. Best to explicitly include your expected output for the sample data you give.



The issue here is that pois.byvar returns a data.frame. So in order for mutate to be able to use the output of pois.byvar we need to store the data.frames in a list.



Here is a tidier version of your code



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))


This creates a column res which contains the data.frame output of pois.byar.



Or perhaps you'd like to unnest the list column to expand entries into different columns?



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95





share|improve this answer























  • Just trying to edit the question to include desired format.

    – damo
    Mar 23 at 22:32






  • 1





    UNNEST. That's exactly what I want. THANK YOU.

    – damo
    Mar 23 at 22:42












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%2f55318712%2fhow-to-add-the-results-of-applying-a-function-to-an-existing-data-frame%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









1














It's not clear to me what your expected output is supposed to me. Your comment does not really help. Best to explicitly include your expected output for the sample data you give.



The issue here is that pois.byvar returns a data.frame. So in order for mutate to be able to use the output of pois.byvar we need to store the data.frames in a list.



Here is a tidier version of your code



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))


This creates a column res which contains the data.frame output of pois.byar.



Or perhaps you'd like to unnest the list column to expand entries into different columns?



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95





share|improve this answer























  • Just trying to edit the question to include desired format.

    – damo
    Mar 23 at 22:32






  • 1





    UNNEST. That's exactly what I want. THANK YOU.

    – damo
    Mar 23 at 22:42
















1














It's not clear to me what your expected output is supposed to me. Your comment does not really help. Best to explicitly include your expected output for the sample data you give.



The issue here is that pois.byvar returns a data.frame. So in order for mutate to be able to use the output of pois.byvar we need to store the data.frames in a list.



Here is a tidier version of your code



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))


This creates a column res which contains the data.frame output of pois.byar.



Or perhaps you'd like to unnest the list column to expand entries into different columns?



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95





share|improve this answer























  • Just trying to edit the question to include desired format.

    – damo
    Mar 23 at 22:32






  • 1





    UNNEST. That's exactly what I want. THANK YOU.

    – damo
    Mar 23 at 22:42














1












1








1







It's not clear to me what your expected output is supposed to me. Your comment does not really help. Best to explicitly include your expected output for the sample data you give.



The issue here is that pois.byvar returns a data.frame. So in order for mutate to be able to use the output of pois.byvar we need to store the data.frames in a list.



Here is a tidier version of your code



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))


This creates a column res which contains the data.frame output of pois.byar.



Or perhaps you'd like to unnest the list column to expand entries into different columns?



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95





share|improve this answer













It's not clear to me what your expected output is supposed to me. Your comment does not really help. Best to explicitly include your expected output for the sample data you give.



The issue here is that pois.byvar returns a data.frame. So in order for mutate to be able to use the output of pois.byvar we need to store the data.frames in a list.



Here is a tidier version of your code



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))


This creates a column res which contains the data.frame output of pois.byar.



Or perhaps you'd like to unnest the list column to expand entries into different columns?



library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 23 at 22:27









Maurits EversMaurits Evers

32.4k41737




32.4k41737












  • Just trying to edit the question to include desired format.

    – damo
    Mar 23 at 22:32






  • 1





    UNNEST. That's exactly what I want. THANK YOU.

    – damo
    Mar 23 at 22:42


















  • Just trying to edit the question to include desired format.

    – damo
    Mar 23 at 22:32






  • 1





    UNNEST. That's exactly what I want. THANK YOU.

    – damo
    Mar 23 at 22:42

















Just trying to edit the question to include desired format.

– damo
Mar 23 at 22:32





Just trying to edit the question to include desired format.

– damo
Mar 23 at 22:32




1




1





UNNEST. That's exactly what I want. THANK YOU.

– damo
Mar 23 at 22:42






UNNEST. That's exactly what I want. THANK YOU.

– damo
Mar 23 at 22:42




















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%2f55318712%2fhow-to-add-the-results-of-applying-a-function-to-an-existing-data-frame%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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해