当使用grepl替换每个匹配的字符串时,如何简化下面的代码?
我列出了一列文件名(data$name),如下所示:当使用grepl替换每个匹配的字符串时,如何简化下面的代码?,r,R,我列出了一列文件名(data$name),如下所示: aaa.doc aaa.pptx http://aaa aaa.jpg guide some memo ... 我想尽可能地用所有类型替换它们,没有特定文件类型的名称我只会将其标记为“others” 用文件类型替换文件名列时,我执行了以下操作: data$name[grepl("http",data$name,ignore.case = FALSE)]<-"web" data$name[grepl("pptx",data$name,i
aaa.doc
aaa.pptx
http://aaa
aaa.jpg
guide
some memo
...
我想尽可能地用所有类型替换它们,没有特定文件类型的名称我只会将其标记为“others”
用文件类型替换文件名列时,我执行了以下操作:
data$name[grepl("http",data$name,ignore.case = FALSE)]<-"web"
data$name[grepl("pptx",data$name,ignore.case = FALSE)]<-"ppt"
data$name[grepl("pdf",data$name,ignore.case = FALSE)]<-"pdf"
data$name[grepl("txt",data$name,ignore.case = FALSE)]<-"txt"
...
data$name[grepl(“http”,data$name,ignore.case=FALSE)]
在tidyverse
中,它的外观可能如下所示。当()
图书馆(tidyverse)
资料
tidyverse
中,它的外观可能如下所示。当()
图书馆(tidyverse)
数据1)定义一个命名向量trans
,将匹配项转换为类型。然后找到trans
名称,并使用stripply
对每个名称进行翻译
Straply
的第一个参数是输入字符向量,第二个是要匹配的模式,第三个是应用于此处使用公式表示法表示的匹配的函数,empty
参数指定在不存在匹配项时使用什么,并且simplify=TRUE
使其输出普通字符向量而不是列表
library(gsubfn)
trans <- c(http = "web", pptx = "ppt", pdf = "pdf", txt = "txt")
pat <- paste(names(trans), collapse = "|") # http|pptx|pdf|txt
strapply(tolower(d$name), pat, ~ trans[x], empty = "others", simplify = TRUE)
## [1] "others" "ppt" "web" "others" "others" "others" "others"
3)R Base-Reduce我们可以使用Reduce
的方法,基本上与for
循环相同,但没有显式循环:
Match <- function(result, nm) ifelse(grepl(nm, result), trans[nm], result)
out <- Reduce(Match, names(trans), init = tolower(d$name))
out[out == tolower(d$name)] <- "others"
out
## [1] "others" "ppt" "web" "others" "others" "others" "others"
Match1)Straply定义一个命名向量trans
,将匹配转换为类型。然后找到trans
名称,并使用stripply
对每个名称进行翻译
Straply
的第一个参数是输入字符向量,第二个是要匹配的模式,第三个是应用于此处使用公式表示法表示的匹配的函数,empty
参数指定在不存在匹配项时使用什么,并且simplify=TRUE
使其输出普通字符向量而不是列表
library(gsubfn)
trans <- c(http = "web", pptx = "ppt", pdf = "pdf", txt = "txt")
pat <- paste(names(trans), collapse = "|") # http|pptx|pdf|txt
strapply(tolower(d$name), pat, ~ trans[x], empty = "others", simplify = TRUE)
## [1] "others" "ppt" "web" "others" "others" "others" "others"
3)R Base-Reduce我们可以使用Reduce
的方法,基本上与for
循环相同,但没有显式循环:
Match <- function(result, nm) ifelse(grepl(nm, result), trans[nm], result)
out <- Reduce(Match, names(trans), init = tolower(d$name))
out[out == tolower(d$name)] <- "others"
out
## [1] "others" "ppt" "web" "others" "others" "others" "others"
Match您可以提供一个data$name
样本与所有相关案例吗?您也可以将data$name
更改为factor
类,只需根据需要更改级别
您可以提供一个data$name
样本与所有相关案例吗?您还可以将data$name
更改为因子
类,只需根据需要更改级别
,非常感谢您的回答!请查看我的更新,当我使用“simplify=TRUE”时,我仍然有一个列表。并且不能正确替换为'empty=“others”“。希望您能检查我的代码。谢谢!我在Windows和Linux上尝试了(1)的代码,在Windows上使用了R3.5.3和一个早期版本的R,在Linux上使用了R3.4.0,他们都给出了答案中所示的结果。在新版本的R上再试一次。如果旧的话,更新R和你的软件包以防万一(尽管我在旧版本的R上运行正常)。非常感谢你的回答!请查看我的更新,当我使用“simplify=TRUE”时,我仍然有一个列表。并且不能正确替换为'empty=“others”“。希望您能检查我的代码。谢谢!我在Windows和Linux上尝试了(1)的代码,在Windows上使用了R3.5.3和一个早期版本的R,在Linux上使用了R3.4.0,他们都给出了答案中所示的结果。在新版本的R上再试一次。如果旧的话,更新R和你的包,以防万一(尽管我在旧版本的R上运行正常)。
d <-
structure(list(name = structure(c(1L, 3L, 5L, 2L, 4L, 7L, 6L), .Label = c("aaa.doc",
"aaa.jpg", "aaa.pptx", "guide", "http://aaa", "memo", "some"),
class = "factor")), class = "data.frame", row.names = c(NA, -7L))