簡體   English   中英

當嵌套列中有 NA 時,如何在 R 中的嵌套 Tibble 上 map function?

[英]How to map a function on a nested Tibble in R when there are NA in nested column?

考慮這樣的 Tibble:

df <- tibble(station = c("station1","station2"), data = c(list(NA), list(tibble(timestamp = c("2001-01-01","2002-01-02", "2002-01-03"), value=c(1,2,3)))))

我現在想要 map function,就像嵌套小標題中時間戳列上的 lubridate::ymd() 一樣。 問題是我在 df-tibble 的父數據列中有 NA 值,需要留在那里。

有可行的解決方案嗎?

非常感謝你!

我用 mutate、mutate_if、map、map_if、else_if 嘗試了幾件事,但對我沒有任何作用。

另一種選擇是將map_ifis.data.frame一起使用:

library(tidyverse)

df <- tibble(
  station = c("station1", "station2"),
  data = c(list(NA), list(tibble(timestamp = c("2001-01-01", "2002-01-02", "2002-01-03"), value = c(1, 2, 3))))
)

df <- df |> 
  mutate(data = map_if(data, is.data.frame, ~ mutate(.x, timestamp = lubridate::ymd(timestamp)))) 

df$data
#> [[1]]
#> [1] NA
#> 
#> [[2]]
#> # A tibble: 3 × 2
#>   timestamp  value
#>   <date>     <dbl>
#> 1 2001-01-01     1
#> 2 2002-01-02     2
#> 3 2002-01-03     3

您可以在映射 function 中使用條件語句:

df2 <- df %>%
  mutate(data = map(data, function(x) {
    if(is.data.frame(x)) mutate(x, timestamp = lubridate::ymd(timestamp))
    else x
    }))

df2
#> # A tibble: 2 x 2
#>    station  data            
#>    <chr>    <list>          
#>  1 station1 <lgl [1]>       
#>  2 station2 <tibble [3 x 2]>

df2$data
#> [[1]]
#> [1] NA
#> 
#> [[2]]
#> # A tibble: 3 x 2
#>   timestamp  value
#>   <date>     <dbl>
#> 1 2001-01-01     1
#> 2 2002-01-02     2
#> 3 2002-01-03     3

nest / unnest的可能解決方案:

> df2 <- df %>% unnest(data) %>% mutate(timestamp = ymd(timestamp)) %>% nest(data = c(timestamp, value))
> df2
# A tibble: 2 × 2
  station  data            
  <chr>    <list>          
1 station1 <tibble [1 × 2]>
2 station2 <tibble [3 × 2]>
> df2$data
[[1]]
# A tibble: 1 × 2
  timestamp value
  <date>    <dbl>
1 NA           NA

[[2]]
# A tibble: 3 × 2
  timestamp  value
  <date>     <dbl>
1 2001-01-01     1
2 2002-01-02     2
3 2002-01-03     3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM