gpt4 book ai didi

重命名环境中的数据集

转载 作者:行者123 更新时间:2023-12-02 17:28:15 25 4
gpt4 key购买 nike

这是我的环境中的内容。在我的环境中,我有一堆 csv 文件作为数据框加载,以月/年的某种组合命名。

enter image description here

我想做的是重命名这些对象,而不是列/行,而是对象本身。

例如,我有兴趣删除“FRL.csv”,或标题每个数据框的字符串的最后 7 个字符。然后我会得到“APR03”、“APR04”、.....、“JUL02”、“JUL03”。

我做了什么。

以下是获取名称列表的方法,这些名称是我环境中对象的标签。

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 = "-")
)

这只是我尝试分配新字符串来替换全局环境中的字符串。

粘贴代码里面是我输入的字符串。

paste(
substr(ls(all.names = TRUE, envir = .GlobalEnv), 1, 3),
substr(ls(all.names = TRUE, envir = .GlobalEnv), 4, 5),
sep = "-")

[1] "APR-01" "APR-02" "APR-03" "APR-04" "APR-05" "APR-06" "APR-07" "APR-08" "APR-09" "JAN-01" "JAN-02" "JAN-03"
[14] "JAN-04" "JAN-05" "JAN-06" "JAN-07" "JAN-08" "JAN-09" "JAN-10" "JUL-01" "JUL-02" "JUL-03" "JUL-04" "JUL-05" "JUL-06"
[27] "JUL-07" "JUL-08" "JUL-09" "JUL-10" "MAR-10" "MAY-10" "NOV-09" "NOV-10" "OCT-01" "OCT-02" "OCT-03" "OCT-04" "OCT-05"
[40] "OCT-06" "OCT-07" "OCT-08" "SEP-09" "SEP-10"

我在运行分配代码时遇到此错误。

Warning message: In assign(ls(all.names = TRUE, envir = .GlobalEnv), paste(substr(ls(all.names = TRUE, : only the first element is used as variable name

而且我环境中的所有数据集都没有重命名。

enter image description here

有人对如何重命名环境 Pane 中的对象或对象本身有什么建议吗?

最佳答案

我会使用一个简单的 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"

关于重命名环境中的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752688/

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