繁体   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