Reputation: 529
I have a tibble/dataframe called sections
that I want to use to create several new tibbles/dataframes. I want to iterate over each row and create a new tibble for each. The first column provides the name of the new tibble and the 2nd and 3rd columns provide the indexes to use on another tibble called my_text
.
sections <- structure(list(sections = c("cash_and_bank_sweep", "money_market_funds_non-sweep",
"equities"),
begin_row = c(325L, 345L, 357L),
end_row = c(345L, 357L, 384L)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -3L))
> sections
# A tibble: 3 x 3
sections begin_row end_row
<chr> <int> <int>
1 cash_and_bank_sweep 325 345
2 money_market_funds_non-sweep 345 357
3 equities 357 384
set.seed(1)
my_text <- tibble(Strings = sample(letters, size = 1000, replace = TRUE)
> head(my_text)
# A tibble: 6 x 1
Strings
<chr>
1 y
2 d
3 g
4 a
5 b
6 w
So the first tibble I want to create would be cash_and_bank_sweep
. Manually I can create as follows:
cash_and_bank_sweep <- tibble(Strings = my_text$Strings[sections$begin_row[1]:sections$end_row[1]])
> head(cash_and_bank_sweep)
# A tibble: 6 x 1
Strings
<chr>
1 e
2 n
3 e
4 k
5 k
6 q
Is there some way to efficiently do this with a loop or other construct?
Upvotes: 2
Views: 698
Reputation: 388817
We can create a sequence between begin_row
and end_row
and get the data in long format and do an inner_join
with my_text
column after adding a row_number()
column.
library(tidyverse)
sections %>%
mutate(value = map2(begin_row, end_row, `:`)) %>%
unnest(value) %>%
select(-begin_row, -end_row) %>%
inner_join(my_text %>% mutate(row = row_number()), by = c('value' = 'row'))
# A tibble: 62 x 3
# sections value Strings
# <chr> <int> <chr>
# 1 cash_and_bank_sweep 325 e
# 2 cash_and_bank_sweep 326 n
# 3 cash_and_bank_sweep 327 e
# 4 cash_and_bank_sweep 328 k
# 5 cash_and_bank_sweep 329 k
# 6 cash_and_bank_sweep 330 q
# 7 cash_and_bank_sweep 331 a
# 8 cash_and_bank_sweep 332 z
# 9 cash_and_bank_sweep 333 m
#10 cash_and_bank_sweep 334 a
# … with 52 more rows
This will return a single dataframe with all the required rows in it, if you need separate dataframes add %>% group_split(sections)
in the chain after the last step i.e inner_join
.
Upvotes: 0
Reputation: 886948
We can use pmap
to create a list
of tibble
s and if we need as individual objects in the global environment (not recommended), use list2env
library(purrr)
lst1 <- pmap(sections[-1], ~ tibble(Strings = my_text$Strings[..1:..2]))
names(lst1) <- sections[[1]]
list2env(lst1, .GlobalEnv)
Or another option is map2
lst1 <- map2(sections$begin_row, sections$end_row,
~ tibble(Strings = my_text$Strings[.x:.y]))
names(lst1) <- sections[[1]]
In base R
, this can be done with Map
lst1 <- Map(function(i, j) data.frame(Strings = my_text$Strings[i:j]),
sections$begin_row, sections$end_row)
names(lst1) <- sections[[1]]
Or using a for
loop
lst1 <- vector('list', nrow(sections))
names(lst1) <- sections[[1]]
for(i in seq_along(lst1)) {
lst1[[i]] <- data.frame(Strings = my_text$Strings[sections$begin_row[i]:sections$end_row[i]])
}
Upvotes: 1