使用make.names或在工作环境中重命名OBEJCT时,用下划线替换点
我正在使用 this (我知道,我知道)解决方案在上传文件夹时自动命名Excel文件。
temp = list.files(pattern="*.csv")
list2env(
lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))),
read.csv), envir = .GlobalEnv)
现在,make.names
使用。
制作正确的名称,而我宁愿使用_
。动态将点更改为强调的最佳方法是什么?
我尝试做类似lapply(ls(),gsub(“。”,“ _”))
之类的事情,但这似乎不起作用。 我也希望在list2env
中进行此操作,但我会选择单独的行。
I am using this (ill advised, I know) solution to automatically name Excel files when uploading a folder.
temp = list.files(pattern="*.csv")
list2env(
lapply(setNames(temp, make.names(gsub("*.csvquot;, "", temp))),
read.csv), envir = .GlobalEnv)
Now, make.names
uses .
to make correct names, while I would rather use _
. What would be the best way to dynamically change the dots to underscores?
I tried to do something like lapply(ls(),gsub(".", "_"))
, but that does not seem to work.
I would also prefer to do it within list2env
, but I'll settle for a separate line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是使用
"."
,它在正则表达式中匹配每个字符。如果要匹配字符串中的.
,则必须使用"\\."
对其进行转义。就我个人而言,当您可以使用一个简单的函数使代码更清晰、更易于理解时,我不喜欢将所有代码都放在一行中。
The issue is probably that use you use
"."
which in a regex matches every character. If you want to match a.
in a string you have to escape it using use"\\."
.Personally I don't like it to wrangle all code in one line when you could use a simple function to make the code cleaner and more understandable.