gpt4 book ai didi

r - str_replace 模式是 ""给出 "Error in mutate_impl(.data, dots) : Evaluation error: Not implemented."

转载 作者:行者123 更新时间:2023-12-01 11:18:07 27 4
gpt4 key购买 nike

我在 df 中有一个功能,其中一些缺失值仅显示为“”。

unique(page_my_df$Type)
[1] "list" "narrative" "how to" "news feature"
[5] "diary" "" "interview"

我想用“未知”替换“”的所有实例。
page_my_df <- page_my_df %>% 
mutate(Type = str_replace(.$Type, "", "unknown"),
Voice = str_replace(.$Voice, "", "unknown"))

Error in mutate_impl(.data, dots) : Evaluation error: Not implemented.



阅读一些文档 here ,特别是在模式下:

Match character, word, line and sentence boundaries with boundary(). An empty pattern, "", is equivalent to boundary("character").



所以我试过:
page_my_df <- page_my_df %>% 
mutate(Type = str_replace(.$Type, boundary(""), "unknown"),
Voice = str_replace(.$Voice, boundary(""), "unknown"))

然后给出:

Error in mutate_impl(.data, dots) : Evaluation error: 'arg' should be one of “character”, “line_break”, “sentence”, “word”.



如何在 dplyr::mutate() 中用“unknown”替换空字符串?

最佳答案

这是一种方法:

library(tidyverse)
library(stringr)

z <- c( "list", "narrative", "how to", "news feature",
"diary", "" , "interview" )

data.frame(element = 1:length(z), Type = z) %>%
mutate(Type = str_replace(Type, "^$", "unknown"))
#output
element Type
1 1 list
2 2 narrative
3 3 how to
4 4 news feature
5 5 diary
6 6 unknown
7 7 interview

也无需使用 .$ 引用 mutate 调用中的数据框

^ 和美元符号 $ 是元字符,分别匹配行首和行尾的空字符串。

关于r - str_replace 模式是 ""给出 "Error in mutate_impl(.data, dots) : Evaluation error: Not implemented.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47903666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com