这是我环境中的内容。我有一堆csv文件作为数据框加载,在我的环境中以月/年的某种组合命名。
我想要做的是重命名这些对象,而不是列/行,而是对象本身。
例如,我有兴趣删除" FRL.csv",或者标题每个数据帧的字符串的最后7个字符。然后我会得到" APR03"," APR04",.....," JUL02"," JUL03"。
我做了什么。
以下是如何获取作为我环境中对象标签的名称列表。
BEGIN
INSERT INTO popayment (purchaseorderid, staffid)
VALUES (:new.purchaseorderid, :new.staffid);
END;
我只是尝试分配新的字符串来替换全局环境中的字符串。
粘贴代码中包含我正在输入的字符串。
ls(all.names = TRUE, envir = .GlobalEnv)
[1] "APR01FRL.csv" "APR02FRL.csv" "APR03FRL.csv" "APR04FRL.csv" "APR05FRL.csv" "APR06FRL.csv" "APR07FRL.csv"
[8] "APR08FRL.csv" "APR09FRL.csv" "JAN01FRL.csv" "JAN02FRL.csv" "JAN03FRL.csv" "JAN04FRL.csv" "JAN05FRL.csv"
[15] "JAN06FRL.csv" "JAN07FRL.csv" "JAN08FRL.csv" "JAN09FRL.csv" "JAN10FRL.csv" "JUL01FRL.csv" "JUL02FRL.csv"
[22] "JUL03FRL.csv" "JUL04FRL.csv" "JUL05FRL.csv" "JUL06FRL.csv" "JUL07FRL.csv" "JUL08FRL.csv" "JUL09FRL.csv"
[29] "JUL10FRL.csv" "MAR10FRL.csv" "MAY10FRL.csv" "NOV09FRL.csv" "NOV10FRL.csv" "OCT01FRL.csv" "OCT02FRL.csv"
[36] "OCT03FRL.csv" "OCT04FRL.csv" "OCT05FRL.csv" "OCT06FRL.csv" "OCT07FRL.csv" "OCT08FRL.csv" "SEP09FRL.csv"
[43] "SEP10FRL.csv"
assign(
ls(all.names = TRUE, envir = .GlobalEnv),
paste(
substr(ls(all.names = TRUE, envir = .GlobalEnv), 1, 3),
substr(ls(all.names = TRUE, envir = .GlobalEnv), 4, 5),
sep = "-")
)
运行分配代码时出现此错误。
警告消息:在assign(ls(all.names = TRUE,envir = .GlobalEnv)中, paste(substr(ls)(all.names = TRUE,:仅使用第一个元素 作为变量名称
并且我的环境中没有任何数据集被重命名。
是否有人就如何在环境窗格中重命名对象或对象本身提出建议?
答案 0 :(得分:2)
我会使用一个简单的for循环:
## create test objects (because I don't have your data.frames)
ns <- c('APR01FRL.csv','APR02FRL.csv','APR03FRL.csv','APR04FRL.csv','APR05FRL.csv','APR06FRL.csv','APR07FRL.csv','APR08FRL.csv','APR09FRL.csv','JAN01FRL.csv','JAN02FRL.csv','JAN03FRL.csv','JAN04FRL.csv','JAN05FRL.csv','JAN06FRL.csv','JAN07FRL.csv','JAN08FRL.csv','JAN09FRL.csv','JAN10FRL.csv','JUL01FRL.csv','JUL02FRL.csv','JUL03FRL.csv','JUL04FRL.csv','JUL05FRL.csv','JUL06FRL.csv','JUL07FRL.csv','JUL08FRL.csv','JUL09FRL.csv','JUL10FRL.csv','MAR10FRL.csv','MAY10FRL.csv','NOV09FRL.csv','NOV10FRL.csv','OCT01FRL.csv','OCT02FRL.csv','OCT03FRL.csv','OCT04FRL.csv','OCT05FRL.csv','OCT06FRL.csv','OCT07FRL.csv','OCT08FRL.csv','SEP09FRL.csv','SEP10FRL.csv');
for (n in ns) assign(n,data.frame());
## retrieve all target data.frames by name pattern
ns <- ls(all.names=T,envir=globalenv(),pattern='FRL\\.csv$');
ns;
## [1] "APR01FRL.csv" "APR02FRL.csv" "APR03FRL.csv" "APR04FRL.csv" "APR05FRL.csv" "APR06FRL.csv" "APR07FRL.csv" "APR08FRL.csv" "APR09FRL.csv" "JAN01FRL.csv" "JAN02FRL.csv" "JAN03FRL.csv" "JAN04FRL.csv" "JAN05FRL.csv" "JAN06FRL.csv" "JAN07FRL.csv" "JAN08FRL.csv" "JAN09FRL.csv" "JAN10FRL.csv" "JUL01FRL.csv" "JUL02FRL.csv" "JUL03FRL.csv" "JUL04FRL.csv" "JUL05FRL.csv" "JUL06FRL.csv" "JUL07FRL.csv" "JUL08FRL.csv" "JUL09FRL.csv" "JUL10FRL.csv" "MAR10FRL.csv" "MAY10FRL.csv" "NOV09FRL.csv" "NOV10FRL.csv" "OCT01FRL.csv" "OCT02FRL.csv" "OCT03FRL.csv" "OCT04FRL.csv" "OCT05FRL.csv" "OCT06FRL.csv" "OCT07FRL.csv" "OCT08FRL.csv" "SEP09FRL.csv" "SEP10FRL.csv"
## rename them with a for-loop
for (n in ns) { n2 <- sub('FRL\\.csv$','',n); assign(n2,get(n)); rm(list=n); };
## show the result
ls(all.names=T,envir=globalenv(),pattern='FRL\\.csv$');
## character(0)
ls(all.names=T,envir=globalenv(),pattern='^[A-Z]{3}[0-9]{2}$');
## [1] "APR01" "APR02" "APR03" "APR04" "APR05" "APR06" "APR07" "APR08" "APR09" "JAN01" "JAN02" "JAN03" "JAN04" "JAN05" "JAN06" "JAN07" "JAN08" "JAN09" "JAN10" "JUL01" "JUL02" "JUL03" "JUL04" "JUL05" "JUL06" "JUL07" "JUL08" "JUL09" "JUL10" "MAR10" "MAY10" "NOV09" "NOV10" "OCT01" "OCT02" "OCT03" "OCT04" "OCT05" "OCT06" "OCT07" "OCT08" "SEP09" "SEP10"
答案 1 :(得分:1)
其中包含for
和assign
的{{1}}循环可用于执行此操作。您可能需要向rm
添加pattern
参数,以避免重命名您不想重命名的对象。
ls