R中需要数字时如何计算字符串?
我想将数字更改为因子,但我的因子级别以字符串形式给出,而函数因子需要数字。如何将其转换为数字或评估字符串?示例:
给定:
a = c(0,1,1,1,0,1,0)
lev = 'c(0,1)'
lab = c('firstlev','secondlev') # this is irrelevant to question.
想要:
factor(a,levels = lev labels = lab)
我可以明确地编写级别,但我有一个大循环,并且正在从另一个文件自动读取因子级别。我尝试了 quo、!!、eval 和朋友,但我完全不知道什么是正确的方法,甚至我应该寻找什么,从字符串到数字的转换,或者字符串的评估类型。
I want to change a numeric into a factor, but my factor level is given as a character string while the function factor needs a numeric. How do I convert it into a numeric or evaluate the character string? Example:
given:
a = c(0,1,1,1,0,1,0)
lev = 'c(0,1)'
lab = c('firstlev','secondlev') # this is irrelevant to question.
wanted:
factor(a,levels = lev labels = lab)
I could write the levels explicitly, but I have a big loop and am reading the factor levels automatically from another file. I tried quo, !!, eval and friends, but I am totally lost as to what is the right way to do and even what I should be looking for, conversion from string to numeric, or for a type of evaluation of a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
由 reprex 包 (v2.0.1) 创建于 2022 年 4 月 4 日< /sup>
How about this:
Created on 2022-04-04 by the reprex package (v2.0.1)