通过使用is_empty(),用na替换字符(0)的列表元素

发布于 2025-01-31 10:24:25 字数 1736 浏览 3 评论 0原文

我有一个dataframe(my_df),其中一行中,带有cartare(0)的列表。

我想通过使用rlang的is_empty()替换na_character。我知道通过使用例如长度(x)== 0有解决方法,但是我试图理解为什么我使用iS_empty()的方法不起作用。感谢任何提示。

library(tidyverse)

my_df <- data.frame(
  stringsAsFactors = FALSE,
              type = c("house", "tent", "treehouse, mainhouse")
) %>% 
  mutate(house=str_extract_all(type, regex("house")))
  
my_df
#>                   type        house
#> 1                house        house
#> 2                 tent             
#> 3 treehouse, mainhouse house, house
class(my_df$house)
#> [1] "list"

my_df %>% 
  mutate(house_mod=modify_if(house,
                             .p = is_empty(house),
                             .f = NA_character_))
#> Error in `mutate()`:
#> ! Problem while computing `house_mod = modify_if(house, .p =
#>   is_empty(house), .f = NA_character_)`.
#> Caused by error in `probe()`:
#> ! length(.p) == length(.x) is not TRUE

my_df <- my_df %>% 
  mutate(house_empty=map(house, ~is_empty(.))) 
my_df
#>                   type        house house_empty
#> 1                house        house       FALSE
#> 2                 tent                     TRUE
#> 3 treehouse, mainhouse house, house       FALSE

my_df %>% 
  mutate(house_mod=modify_if(house,
                             .p = house_empty==T,
                             .f = NA_character_))
#> Error in `mutate()`:
#> ! Problem while computing `house_mod = modify_if(house, .p = house_empty
#>   == T, .f = NA_character_)`.
#> ✖ `house_mod` must be size 3 or 1, not 2.

I have a dataframe (my_df) with a list called 'house' with character(0) in one row.

I would like to replace this value with NA_character by using rlang's is_empty(). I am aware that there are workarounds by using e.g. length(x)==0, but I am trying to understand why my approach with is_empty() does not work. Grateful for any hint.

library(tidyverse)

my_df <- data.frame(
  stringsAsFactors = FALSE,
              type = c("house", "tent", "treehouse, mainhouse")
) %>% 
  mutate(house=str_extract_all(type, regex("house")))
  
my_df
#>                   type        house
#> 1                house        house
#> 2                 tent             
#> 3 treehouse, mainhouse house, house
class(my_df$house)
#> [1] "list"

my_df %>% 
  mutate(house_mod=modify_if(house,
                             .p = is_empty(house),
                             .f = NA_character_))
#> Error in `mutate()`:
#> ! Problem while computing `house_mod = modify_if(house, .p =
#>   is_empty(house), .f = NA_character_)`.
#> Caused by error in `probe()`:
#> ! length(.p) == length(.x) is not TRUE

my_df <- my_df %>% 
  mutate(house_empty=map(house, ~is_empty(.))) 
my_df
#>                   type        house house_empty
#> 1                house        house       FALSE
#> 2                 tent                     TRUE
#> 3 treehouse, mainhouse house, house       FALSE

my_df %>% 
  mutate(house_mod=modify_if(house,
                             .p = house_empty==T,
                             .f = NA_character_))
#> Error in `mutate()`:
#> ! Problem while computing `house_mod = modify_if(house, .p = house_empty
#>   == T, .f = NA_character_)`.
#> ✖ `house_mod` must be size 3 or 1, not 2.

Created on 2022-05-23 by the reprex package (v2.0.1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-02-07 10:24:25

.f参数应采用一个函数,但是您刚刚通过na_character _。尝试〜na_character _

my_df %>% 
     mutate(house_mod=modify_if(house,
                                .p = is_empty,
                                .f = ~NA_character_))
#>                   type        house    house_mod
#> 1                house        house        house
#> 2                 tent                        NA
#> 3 treehouse, mainhouse house, house house, house

The .f argument should take a function, but you have just passed NA_character_ directly. Try ~NA_character_ :

my_df %>% 
     mutate(house_mod=modify_if(house,
                                .p = is_empty,
                                .f = ~NA_character_))
#>                   type        house    house_mod
#> 1                house        house        house
#> 2                 tent                        NA
#> 3 treehouse, mainhouse house, house house, house
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文