r-我如何通过管道或不通过数据帧运行for循环来清理这个问题?
发布时间:2022-04-15 17:33:59 199
相关标签:
我目前执行这段代码来清理一些excel文件。我想知道用什么样的管道来清理这个问题,甚至可以通过使用purrr和map函数的列表来实现同样的效果。这个代码基本上
- 读取excel文件
- 删除不需要的特殊字符
- 根据某些条件写入指定的文件夹
非常感谢。
files <-
list.files(path = "folder1/", pattern = "*xls")
for (i in 1:length(files)) {
files1 <-
basename(files[i])
files1 <- gsub(pattern = "'", replacement = "" , x = files1)
files1 <- gsub(pattern = "\\*", replacement = "" , x = files1)
data <-
read_excel(paste0("folder1/", files[i]))
data$column1 <- as.character(data$column1)
data <- data %>%
mutate(charCount = nchar(data$column1))
data <- data %>% mutate(across(where(is.character), str_trim))
if(any(grepl(pattern = "10", x = data$charCount) == FALSE) || any(grepl(pattern = "Coolguy",x = data$column5,ignore.case = T) == TRUE)) {
write_xlsx(data, path = (paste0("bad/", files1, "x")))
}
if (any(grepl(pattern = "10", x = data$charCount) == TRUE) && any(grepl(pattern = "Coolerguy",x = data$column4,ignore.case = T) == TRUE)){
write_xlsx(data[,1:11], path = (paste0("good/", files1, "x")))
}else{
write_xlsx(data[,1:11], path = (paste0("bad/", files1, "x")))
}}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报