1
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?

nestされたdataをmutateによるデータ解析の備忘録

Last updated at Posted at 2025-09-15

いつもnested_dataのmapの使い方を忘れるので備忘録

準備

pak::pak("palmerpenguins")

library(tidyverse)
library(palmerpenguins)

data_penguins <- penguins
> str(data_penguins)
tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
 $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
 $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
 $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
 $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
 $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
 $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...

入れ子構造のデータから、文字列を取り出して、重複を削除してから、ひとつにつなげる

speciesでnestした後、species毎のislandの文字列を抜き出し、重複を削除してから、つなげる。


nested_data <- data_penguins |>
  group_by(species) |>
  nest() |>
  mutate(
    islands_str_c = map_chr(
      data,
      ~ str_c(.x$island |> unique(), collapse = ", ")
    ),
    islands_paste0 = map_chr(
      data,
      ~ paste0(.x$island |> unique(), collapse = ", ")
    ),
  )

1
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
1
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?