R中导入数据调用split函数报‘对象未找到’,手动构框正常
问题:split函数处理导入数据时出现“对象未找到”错误
我尝试了所有能想到的方法,但在对某列应用split函数时仍持续出现“对象未找到”错误,该问题仅在处理导入的数据文件时出现,在R Studio中手动构建的数据框上执行相同操作则完全正常。我尝试将目标列从字符型转换为数值型,且在排查过程中使用glimpse函数确认,导入数据与手动构建数据框的对应列数据类型有时是一致的。
可正常运行的测试代码
以下代码用于统计图书馆联盟中同一书名(图书)同时借出的最大副本数,可正常运行:
library(tidyverse) options(scipen = 999) options(digits = 22) book_id <- c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B") start_date <- ymd(c("2017-10-28", "2017-11-17", "2018-01-16", "2018-03-10", "2018-04-04", "2018-04-07", "2018-05-05", "2018-06-06", "2018-06-06", "2018-06-28", "2017-11-03", "2017-11-14", "2019-04-01", "2021-05-11", "2022-01-09", "2022-07-11")) end_date <- ymd(c("2017-11-17", "2018-06-06", "2018-01-30", "2018-04-25", "2018-04-10", "2018-05-10", "2018-06-06", "2018-06-06", "2018-06-26", "2018-07-12", "2017-12-06", "2017-12-18", "2019-04-12", "2023-01-05", "2022-12-12", "2023-05-22")) interval_date <- interval(start_date, end_date) df_test <- data.frame(book_id, start_date, end_date, interval_date) df_test_2 <- df_test %>% split(book_id) %>% map_dfr(\(x) mutate(x, max_overlap = max(map_int(seq(min(start_date), max(end_date), 1), \(d) sum(d %within% x$interval_date)))))
该数据框代表2个唯一书名(A和B)及其流通的起止日期。我也曾用“1”和“2”替换“A”和“B”进行测试,同样可以正常运行。
出错的导入数据处理代码
当我导入数据并运行以下代码时,在尝试对OCLC_Number列执行split操作时出现“对象未找到”错误,这是该列唯一出现错误的场景:
df <- read.delim("./Resource_Sharing_Lending_Sample.txt", header = T, na.strings = "", quote = "", stringsAsFactors = F) df2 <- df %>% mutate(OCLC_Number = str_replace_all(OCLC_Number, regex(";.*"), "")) %>% mutate(OCLC_Number = str_replace_all(OCLC_Number, regex(".*?([0-9]+).*"), "\\1")) %>% mutate(OCLC_Number = as.numeric(as.character(OCLC_Number))) %>% mutate(Item_Sent_Date = str_replace_all(Item_Sent_Date, regex(" .*"), "")) %>% mutate(Item_Sent_Date = ymd(Item_Sent_Date)) %>% mutate(Item_Return_Date = str_replace_all(Item_Return_Date, regex(" .*"), "")) %>% mutate(Item_Return_Date = ymd(Item_Return_Date)) %>% mutate(Interval_Date = interval(Item_Sent_Date, Item_Return_Date)) %>% arrange(OCLC_Number, Item_Sent_Date) df2 %>% split(OCLC_Number) %>% map_dfr(\(x) mutate(x, max_overlap = max(map_int(seq(min(Item_Sent_Date), max(Item_Return_Date), 1), \(d) sum(d %within% x$Interval_Date)))))
导入的制表符分隔数据
OCLC_Number Item_Sent_Date Item_Return_Date 100000310 2022-11-03 21:43:09 2023-01-10 11:11:29 1000023983 2019-09-26 20:52:24 2019-11-12 10:12:30 1000023983 2021-12-09 20:38:17 2023-07-12 10:53:16 1000050643 2019-10-14 15:39:01 2020-01-03 10:01:34 1000050643 2020-08-20 16:13:00 2021-05-17 09:37:18 1000084050 2017-11-17 08:36:34 2018-06-06 15:22:18 1000084050 2018-01-16 08:23:16 2018-01-30 10:05:31 1000084050 2018-03-22 08:52:10 2018-04-05 09:56:06 1000084050 2018-05-03 21:44:42 2018-05-10 09:45:45 1000084050 2018-05-29 08:41:59 2018-06-06 13:55:03 1000084050 2018-06-06 14:16:40 2018-06-06 15:07:42 1000084050 2018-06-06 15:12:52 2018-06-26 11:15:41 1000084050 2018-06-28 08:18:35 2018-07-12 09:52:33 1000150454 2022-03-01 10:02:26 2022-04-21 10:46:02 1000150454 2022-05-15 20:32:10 2022-08-01 10:42:43
dput(df2)输出
structure(list(OCLC_Number = c(100000310, 1000023983, 1000023983, 1000050643, 1000050643, 1000084050, 1000084050, 1000084050, 1000084050, 1000084050, 1000084050, 1000084050, 1000084050, 1000150454, 1000150454 ), Item_Sent_Date = structure(c(19299, 18165, 18970, 18183, 18494, 17487, 17547, 17612, 17654, 17680, 17688, 17688, 17710, 19052, 19127), class = "Date"), Item_Return_Date = structure(c(19367, 18212, 19550, 18264, 18764, 17688, 17561, 17626, 17661, 17688, 17688, 17708, 17724, 19103, 19205), class = "Date"), Interval_Date = new("Interval", .Data = c(5875200, 4060800, 50112000, 6998400, 23328000, 17366400, 1209600, 1209600, 604800, 691200, 0, 1728000, 1209600, 4406400, 6739200), start = structure(c(1667433600, 1569456000, 1639008000, 1571011200, 1597881600, 1510876800, 1516060800, 1521676800, 1525305600, 1527552000, 1528243200, 1528243200, 1530144000, 1646092800, 1652572800), tzone = "UTC", class = c("POSIXct", "POSIXt")), tzone = "UTC")), class = "data.frame", row.names = c(NA, -15L))
恳请各位提供帮助,另外这是我的第一篇帖子,若有需要改进之处也请告知。谢谢!
内容的提问来源于stack exchange,提问作者Joe_T
相关产品推荐
相关产品推荐

