0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

data.frameを引数としてdata.frameを出力する関数をgroup毎に実行する方法

Posted at

問題

まず、以下のようなdata.frameを作成します。

set.seed(2)

df <- 
  tibble(
    group = c('A','A','A','A','B','B','B'),
    period = c(1,2,3,4,1,2,3),
    unif = runif(7)
  )

次に、data.frameを引数とし、data.frameを出力とする自作関数を作成します。この関数をgroup毎に実行し、 出力された結果を縦につなげたdata.frameを得たいとします。

example_func <- function(df) {
  num_row <- nrow(df)
  df <- df %>% 
    dplyr::mutate(step = 1)
  for (i in 1:(num_row-1)) {
    df$step[i+1] <- (df$unif[i] > 0.5) + df$step[i]
  }
  return(df)
}

解決策

group_splitとmap_dfrを組み合わせることで求めた結果が得られます。

df %>%
  dplyr::group_split(group) %>%
  purrr::map_dfr(example_func)

# A tibble: 7 × 4
# group period  unif  step
# <chr>  <dbl> <dbl> <dbl>
# 1 A          1 0.185     1
# 2 A          2 0.702     1
# 3 A          3 0.573     2
# 4 A          4 0.168     3
# 5 B          1 0.944     1
# 6 B          2 0.943     2
# 7 B          3 0.129     3

別解

また、以下の方法でも同様の結果が得られます。

df %>%
  split(.$group) %>%
  purrr::map_dfr(example_func)
df %>%
  dplyr::group_nest(group) %>%
  dplyr::mutate(data = purrr::map(data, example_func)) %>%
  tidyr::unnest(data)
df %>%
  dplyr::group_by(group) %>%
  dplyr::group_modify(~example_func(.x))

謝辞

本内容は、r-wakalangにて教えていただきました。heavywatalさん、atusyさんありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?