R, Recursively add text from one row to another by group
I want to recursively add text from one row to another using information stored in another column. Below is a sample dataset:
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,3,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
Which Gives:
# A tibble: 7 x 5
person meal food dependencies solo_meal
<chr> <int> <chr> <dbl> <dbl>
1 A 1 Chicken NA 1
2 A 2 Beef NA 1
3 A 3 Soup and meal 2 2 0
4 A 4 Lamb and meal 3 3 1
5 B 1 Lamb NA 1
6 B 2 Salad and meal 1 1 0
7 B 3 Beef NA 1
I want to add a column (preferably using tidyverse) that combines the food column to look like this:
combined_meal
<chr>
1 Chicken
2 Beef
3 Soup and Beef
4 Lamb and Soup and Beef
5 Lamb
6 Salad and Lamb
7 Beef
I am thinking that this needs to be done recursively, but I don't know how to do that in the tidyverse (or base for that matter).
Thanks for the help!
r parsing tidyverse
add a comment |
I want to recursively add text from one row to another using information stored in another column. Below is a sample dataset:
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,3,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
Which Gives:
# A tibble: 7 x 5
person meal food dependencies solo_meal
<chr> <int> <chr> <dbl> <dbl>
1 A 1 Chicken NA 1
2 A 2 Beef NA 1
3 A 3 Soup and meal 2 2 0
4 A 4 Lamb and meal 3 3 1
5 B 1 Lamb NA 1
6 B 2 Salad and meal 1 1 0
7 B 3 Beef NA 1
I want to add a column (preferably using tidyverse) that combines the food column to look like this:
combined_meal
<chr>
1 Chicken
2 Beef
3 Soup and Beef
4 Lamb and Soup and Beef
5 Lamb
6 Salad and Lamb
7 Beef
I am thinking that this needs to be done recursively, but I don't know how to do that in the tidyverse (or base for that matter).
Thanks for the help!
r parsing tidyverse
add a comment |
I want to recursively add text from one row to another using information stored in another column. Below is a sample dataset:
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,3,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
Which Gives:
# A tibble: 7 x 5
person meal food dependencies solo_meal
<chr> <int> <chr> <dbl> <dbl>
1 A 1 Chicken NA 1
2 A 2 Beef NA 1
3 A 3 Soup and meal 2 2 0
4 A 4 Lamb and meal 3 3 1
5 B 1 Lamb NA 1
6 B 2 Salad and meal 1 1 0
7 B 3 Beef NA 1
I want to add a column (preferably using tidyverse) that combines the food column to look like this:
combined_meal
<chr>
1 Chicken
2 Beef
3 Soup and Beef
4 Lamb and Soup and Beef
5 Lamb
6 Salad and Lamb
7 Beef
I am thinking that this needs to be done recursively, but I don't know how to do that in the tidyverse (or base for that matter).
Thanks for the help!
r parsing tidyverse
I want to recursively add text from one row to another using information stored in another column. Below is a sample dataset:
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,3,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
Which Gives:
# A tibble: 7 x 5
person meal food dependencies solo_meal
<chr> <int> <chr> <dbl> <dbl>
1 A 1 Chicken NA 1
2 A 2 Beef NA 1
3 A 3 Soup and meal 2 2 0
4 A 4 Lamb and meal 3 3 1
5 B 1 Lamb NA 1
6 B 2 Salad and meal 1 1 0
7 B 3 Beef NA 1
I want to add a column (preferably using tidyverse) that combines the food column to look like this:
combined_meal
<chr>
1 Chicken
2 Beef
3 Soup and Beef
4 Lamb and Soup and Beef
5 Lamb
6 Salad and Lamb
7 Beef
I am thinking that this needs to be done recursively, but I don't know how to do that in the tidyverse (or base for that matter).
Thanks for the help!
r parsing tidyverse
r parsing tidyverse
edited Nov 14 '18 at 23:07
JoeShmo
asked Nov 14 '18 at 16:38
JoeShmoJoeShmo
184
184
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Here is a solution using a while loop.
library(dplyr)
library(stringr)
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,NA,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
df2 <- df %>% mutate(combined_meal = food)
while(any(grepl("meal",df2$combined_meal)))
df2 <- df2 %>%
group_by(person) %>%
mutate(mls = str_extract(combined_meal,"\d"),
f = combined_meal[strtoi(mls)],
combined_meal = str_replace(combined_meal,"meal (\d)",f)) %>%
select(-mls,-f) %>%
ungroup()
df2
#> # A tibble: 7 x 6
#> person meal food dependencies solo_meal combined_meal
#> <chr> <int> <chr> <dbl> <dbl> <chr>
#> 1 A 1 Chicken NA 1 Chicken
#> 2 A 2 Beef NA 1 Beef
#> 3 A 3 Soup and meal 2 2 0 Soup and Beef
#> 4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#> 5 B 1 Lamb NA 1 Lamb
#> 6 B 2 Salad and meal~ 1 0 Salad and Lamb
#> 7 B 3 Beef NA 1 Beef
add a comment |
We can try this with accumulate
library(tidyverse)
map_df(split(df, df$person), function(x)
i = which(grepl('meal', x$food))
i1 <- i[1] -1
i2 <- i[length(i)]
v1 <- x$food[i1:i2]
v1 <- sub('and\s*$', "",
sub("meal\s+\d+", "", v1)) %>%
accumulate(., c) %>%
map_chr(~ .x %>%
rev %>%
paste(., collapse = ' and '))
x$combined_meal <- x$food
x$combined_meal[i1:i2] <- v1
x)
# A tibble: 7 x 6
# person meal food dependencies solo_meal combined_meal
# <chr> <int> <chr> <dbl> <dbl> <chr>
#1 A 1 Chicken NA 1 Chicken
#2 A 2 Beef NA 1 Beef
#3 A 3 Soup and meal 2 2 0 Soup and Beef
#4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#5 B 1 Lamb NA 1 Lamb
#6 B 2 Salad and meal 1 1 0 Salad and Lamb
#7 B 3 Beef NA 1 Beef
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%2f53304919%2fr-recursively-add-text-from-one-row-to-another-by-group%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a solution using a while loop.
library(dplyr)
library(stringr)
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,NA,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
df2 <- df %>% mutate(combined_meal = food)
while(any(grepl("meal",df2$combined_meal)))
df2 <- df2 %>%
group_by(person) %>%
mutate(mls = str_extract(combined_meal,"\d"),
f = combined_meal[strtoi(mls)],
combined_meal = str_replace(combined_meal,"meal (\d)",f)) %>%
select(-mls,-f) %>%
ungroup()
df2
#> # A tibble: 7 x 6
#> person meal food dependencies solo_meal combined_meal
#> <chr> <int> <chr> <dbl> <dbl> <chr>
#> 1 A 1 Chicken NA 1 Chicken
#> 2 A 2 Beef NA 1 Beef
#> 3 A 3 Soup and meal 2 2 0 Soup and Beef
#> 4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#> 5 B 1 Lamb NA 1 Lamb
#> 6 B 2 Salad and meal~ 1 0 Salad and Lamb
#> 7 B 3 Beef NA 1 Beef
add a comment |
Here is a solution using a while loop.
library(dplyr)
library(stringr)
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,NA,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
df2 <- df %>% mutate(combined_meal = food)
while(any(grepl("meal",df2$combined_meal)))
df2 <- df2 %>%
group_by(person) %>%
mutate(mls = str_extract(combined_meal,"\d"),
f = combined_meal[strtoi(mls)],
combined_meal = str_replace(combined_meal,"meal (\d)",f)) %>%
select(-mls,-f) %>%
ungroup()
df2
#> # A tibble: 7 x 6
#> person meal food dependencies solo_meal combined_meal
#> <chr> <int> <chr> <dbl> <dbl> <chr>
#> 1 A 1 Chicken NA 1 Chicken
#> 2 A 2 Beef NA 1 Beef
#> 3 A 3 Soup and meal 2 2 0 Soup and Beef
#> 4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#> 5 B 1 Lamb NA 1 Lamb
#> 6 B 2 Salad and meal~ 1 0 Salad and Lamb
#> 7 B 3 Beef NA 1 Beef
add a comment |
Here is a solution using a while loop.
library(dplyr)
library(stringr)
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,NA,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
df2 <- df %>% mutate(combined_meal = food)
while(any(grepl("meal",df2$combined_meal)))
df2 <- df2 %>%
group_by(person) %>%
mutate(mls = str_extract(combined_meal,"\d"),
f = combined_meal[strtoi(mls)],
combined_meal = str_replace(combined_meal,"meal (\d)",f)) %>%
select(-mls,-f) %>%
ungroup()
df2
#> # A tibble: 7 x 6
#> person meal food dependencies solo_meal combined_meal
#> <chr> <int> <chr> <dbl> <dbl> <chr>
#> 1 A 1 Chicken NA 1 Chicken
#> 2 A 2 Beef NA 1 Beef
#> 3 A 3 Soup and meal 2 2 0 Soup and Beef
#> 4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#> 5 B 1 Lamb NA 1 Lamb
#> 6 B 2 Salad and meal~ 1 0 Salad and Lamb
#> 7 B 3 Beef NA 1 Beef
Here is a solution using a while loop.
library(dplyr)
library(stringr)
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,NA,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
df2 <- df %>% mutate(combined_meal = food)
while(any(grepl("meal",df2$combined_meal)))
df2 <- df2 %>%
group_by(person) %>%
mutate(mls = str_extract(combined_meal,"\d"),
f = combined_meal[strtoi(mls)],
combined_meal = str_replace(combined_meal,"meal (\d)",f)) %>%
select(-mls,-f) %>%
ungroup()
df2
#> # A tibble: 7 x 6
#> person meal food dependencies solo_meal combined_meal
#> <chr> <int> <chr> <dbl> <dbl> <chr>
#> 1 A 1 Chicken NA 1 Chicken
#> 2 A 2 Beef NA 1 Beef
#> 3 A 3 Soup and meal 2 2 0 Soup and Beef
#> 4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#> 5 B 1 Lamb NA 1 Lamb
#> 6 B 2 Salad and meal~ 1 0 Salad and Lamb
#> 7 B 3 Beef NA 1 Beef
edited Nov 14 '18 at 17:46
answered Nov 14 '18 at 17:37
jasbnerjasbner
2,026619
2,026619
add a comment |
add a comment |
We can try this with accumulate
library(tidyverse)
map_df(split(df, df$person), function(x)
i = which(grepl('meal', x$food))
i1 <- i[1] -1
i2 <- i[length(i)]
v1 <- x$food[i1:i2]
v1 <- sub('and\s*$', "",
sub("meal\s+\d+", "", v1)) %>%
accumulate(., c) %>%
map_chr(~ .x %>%
rev %>%
paste(., collapse = ' and '))
x$combined_meal <- x$food
x$combined_meal[i1:i2] <- v1
x)
# A tibble: 7 x 6
# person meal food dependencies solo_meal combined_meal
# <chr> <int> <chr> <dbl> <dbl> <chr>
#1 A 1 Chicken NA 1 Chicken
#2 A 2 Beef NA 1 Beef
#3 A 3 Soup and meal 2 2 0 Soup and Beef
#4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#5 B 1 Lamb NA 1 Lamb
#6 B 2 Salad and meal 1 1 0 Salad and Lamb
#7 B 3 Beef NA 1 Beef
add a comment |
We can try this with accumulate
library(tidyverse)
map_df(split(df, df$person), function(x)
i = which(grepl('meal', x$food))
i1 <- i[1] -1
i2 <- i[length(i)]
v1 <- x$food[i1:i2]
v1 <- sub('and\s*$', "",
sub("meal\s+\d+", "", v1)) %>%
accumulate(., c) %>%
map_chr(~ .x %>%
rev %>%
paste(., collapse = ' and '))
x$combined_meal <- x$food
x$combined_meal[i1:i2] <- v1
x)
# A tibble: 7 x 6
# person meal food dependencies solo_meal combined_meal
# <chr> <int> <chr> <dbl> <dbl> <chr>
#1 A 1 Chicken NA 1 Chicken
#2 A 2 Beef NA 1 Beef
#3 A 3 Soup and meal 2 2 0 Soup and Beef
#4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#5 B 1 Lamb NA 1 Lamb
#6 B 2 Salad and meal 1 1 0 Salad and Lamb
#7 B 3 Beef NA 1 Beef
add a comment |
We can try this with accumulate
library(tidyverse)
map_df(split(df, df$person), function(x)
i = which(grepl('meal', x$food))
i1 <- i[1] -1
i2 <- i[length(i)]
v1 <- x$food[i1:i2]
v1 <- sub('and\s*$', "",
sub("meal\s+\d+", "", v1)) %>%
accumulate(., c) %>%
map_chr(~ .x %>%
rev %>%
paste(., collapse = ' and '))
x$combined_meal <- x$food
x$combined_meal[i1:i2] <- v1
x)
# A tibble: 7 x 6
# person meal food dependencies solo_meal combined_meal
# <chr> <int> <chr> <dbl> <dbl> <chr>
#1 A 1 Chicken NA 1 Chicken
#2 A 2 Beef NA 1 Beef
#3 A 3 Soup and meal 2 2 0 Soup and Beef
#4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#5 B 1 Lamb NA 1 Lamb
#6 B 2 Salad and meal 1 1 0 Salad and Lamb
#7 B 3 Beef NA 1 Beef
We can try this with accumulate
library(tidyverse)
map_df(split(df, df$person), function(x)
i = which(grepl('meal', x$food))
i1 <- i[1] -1
i2 <- i[length(i)]
v1 <- x$food[i1:i2]
v1 <- sub('and\s*$', "",
sub("meal\s+\d+", "", v1)) %>%
accumulate(., c) %>%
map_chr(~ .x %>%
rev %>%
paste(., collapse = ' and '))
x$combined_meal <- x$food
x$combined_meal[i1:i2] <- v1
x)
# A tibble: 7 x 6
# person meal food dependencies solo_meal combined_meal
# <chr> <int> <chr> <dbl> <dbl> <chr>
#1 A 1 Chicken NA 1 Chicken
#2 A 2 Beef NA 1 Beef
#3 A 3 Soup and meal 2 2 0 Soup and Beef
#4 A 4 Lamb and meal 3 NA 1 Lamb and Soup and Beef
#5 B 1 Lamb NA 1 Lamb
#6 B 2 Salad and meal 1 1 0 Salad and Lamb
#7 B 3 Beef NA 1 Beef
edited Nov 14 '18 at 18:17
answered Nov 14 '18 at 17:36
akrunakrun
414k13202275
414k13202275
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%2f53304919%2fr-recursively-add-text-from-one-row-to-another-by-group%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