Map nested data by row r
I have data that look like this (thanks once again dput!):
dat <- structure(list(vars = c("var_1", "var_2"), data = list(structure(list(
time = 1:10, value = c(1:10
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(time = 1:10, value = c(11:20
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))), mu = c(1, 2), stdev = c(1,2)), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-2L))
I am trying to mutate an extra column which maps a function over each row. e.g calculate dnorm for each element of the nested variable in dat$data[[1]]$value
using dat$mu[1]
and dat$stdev[1]
and the go on to do the same for row two.
The column I would like to mutate is a tibble [10 x 1] for each row containing this as the output:
dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
Things I have tried that don't work but might be close?:
# This alternates between mean and stdev for each element of each nested variable
dat_1 <- dat %>%
mutate(z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
# apply by row has structure issues
dat_2 <- dat %>%
apply(MARGIN = 1, function(x)
mutate(x, z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
)
a basic map function like this dat_3 <- dat %>% mutate(sigma = map(data, ~ sum(.x$value)))
works fine without referencing other values in the df. This is early days for me using nested data and map in this way - been looking at the documentation for all the map functions to try solve this but no luck yet! If that's clear as mud I can try clarify - thanks in advance!
r nested apply purrr
add a comment |
I have data that look like this (thanks once again dput!):
dat <- structure(list(vars = c("var_1", "var_2"), data = list(structure(list(
time = 1:10, value = c(1:10
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(time = 1:10, value = c(11:20
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))), mu = c(1, 2), stdev = c(1,2)), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-2L))
I am trying to mutate an extra column which maps a function over each row. e.g calculate dnorm for each element of the nested variable in dat$data[[1]]$value
using dat$mu[1]
and dat$stdev[1]
and the go on to do the same for row two.
The column I would like to mutate is a tibble [10 x 1] for each row containing this as the output:
dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
Things I have tried that don't work but might be close?:
# This alternates between mean and stdev for each element of each nested variable
dat_1 <- dat %>%
mutate(z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
# apply by row has structure issues
dat_2 <- dat %>%
apply(MARGIN = 1, function(x)
mutate(x, z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
)
a basic map function like this dat_3 <- dat %>% mutate(sigma = map(data, ~ sum(.x$value)))
works fine without referencing other values in the df. This is early days for me using nested data and map in this way - been looking at the documentation for all the map functions to try solve this but no luck yet! If that's clear as mud I can try clarify - thanks in advance!
r nested apply purrr
add a comment |
I have data that look like this (thanks once again dput!):
dat <- structure(list(vars = c("var_1", "var_2"), data = list(structure(list(
time = 1:10, value = c(1:10
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(time = 1:10, value = c(11:20
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))), mu = c(1, 2), stdev = c(1,2)), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-2L))
I am trying to mutate an extra column which maps a function over each row. e.g calculate dnorm for each element of the nested variable in dat$data[[1]]$value
using dat$mu[1]
and dat$stdev[1]
and the go on to do the same for row two.
The column I would like to mutate is a tibble [10 x 1] for each row containing this as the output:
dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
Things I have tried that don't work but might be close?:
# This alternates between mean and stdev for each element of each nested variable
dat_1 <- dat %>%
mutate(z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
# apply by row has structure issues
dat_2 <- dat %>%
apply(MARGIN = 1, function(x)
mutate(x, z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
)
a basic map function like this dat_3 <- dat %>% mutate(sigma = map(data, ~ sum(.x$value)))
works fine without referencing other values in the df. This is early days for me using nested data and map in this way - been looking at the documentation for all the map functions to try solve this but no luck yet! If that's clear as mud I can try clarify - thanks in advance!
r nested apply purrr
I have data that look like this (thanks once again dput!):
dat <- structure(list(vars = c("var_1", "var_2"), data = list(structure(list(
time = 1:10, value = c(1:10
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(time = 1:10, value = c(11:20
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))), mu = c(1, 2), stdev = c(1,2)), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-2L))
I am trying to mutate an extra column which maps a function over each row. e.g calculate dnorm for each element of the nested variable in dat$data[[1]]$value
using dat$mu[1]
and dat$stdev[1]
and the go on to do the same for row two.
The column I would like to mutate is a tibble [10 x 1] for each row containing this as the output:
dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
Things I have tried that don't work but might be close?:
# This alternates between mean and stdev for each element of each nested variable
dat_1 <- dat %>%
mutate(z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
# apply by row has structure issues
dat_2 <- dat %>%
apply(MARGIN = 1, function(x)
mutate(x, z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
)
a basic map function like this dat_3 <- dat %>% mutate(sigma = map(data, ~ sum(.x$value)))
works fine without referencing other values in the df. This is early days for me using nested data and map in this way - been looking at the documentation for all the map functions to try solve this but no luck yet! If that's clear as mud I can try clarify - thanks in advance!
r nested apply purrr
r nested apply purrr
asked Nov 13 '18 at 3:36
QAsenaQAsena
505
505
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
We can use a parallel map:
library(purrr)
library(dplyr)
expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
out <-
dat %>%
mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))
all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
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%2f53273420%2fmap-nested-data-by-row-r%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
We can use a parallel map:
library(purrr)
library(dplyr)
expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
out <-
dat %>%
mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))
all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
add a comment |
We can use a parallel map:
library(purrr)
library(dplyr)
expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
out <-
dat %>%
mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))
all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
add a comment |
We can use a parallel map:
library(purrr)
library(dplyr)
expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
out <-
dat %>%
mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))
all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE
We can use a parallel map:
library(purrr)
library(dplyr)
expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])
out <-
dat %>%
mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))
all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE
edited Nov 13 '18 at 15:24
answered Nov 13 '18 at 15:07
AurèleAurèle
6,42111533
6,42111533
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
add a comment |
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
Amazing, thank you! I was struggling to understand pmap, makes much more sense now!
– QAsena
Nov 13 '18 at 19:59
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%2f53273420%2fmap-nested-data-by-row-r%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