从列表中获取 data.frame 并从字符串中调用对象
我想复制 Tsay 的金融时间序列书中的表格,并意识到即使我能够做到这一点,我的代码似乎很笨拙并且充满了不良做法。 我尝试了一些 plyr 但并没有真正为我工作。
所以这是我的代码:
library(fBasics)
url= 'http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/d-ibm3dx7008.txt'
table1.2 = read.table(url,header=T)
l1=lapply(table1.2,basicStats)
naml1 = names(l1)
datmat = as.data.frame(matrix(0,nrow=nrow(l1$Date),ncol=4))
nams = names(l1)
j=1
for( i in nams){
datmat[,j] = eval(parse(text=paste("l1",i,sep="$")))
j=j+1
}
rownames(datmat)= rownames(l1[[1]])
colnames(datmat)=colnames(table1.2)
我不关心日期的汇总统计信息,所以只需删除它即可。
datmat =datmat[,-1]
所以我听说应该尽可能避免 eval(parse(text= )。我尝试使用函数 get 但没有工作。
我只是想开始摆脱不良的编程习惯,所以任何建议都非常受欢迎。
I wanted to replicate a table from Tsay's financial time series book and realized that even though I am able to do it, my code seems clumsy and fill with bad practices.
I tried a bit with plyr but didn't really work for me.
So this is my code:
library(fBasics)
url= 'http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/d-ibm3dx7008.txt'
table1.2 = read.table(url,header=T)
l1=lapply(table1.2,basicStats)
naml1 = names(l1)
datmat = as.data.frame(matrix(0,nrow=nrow(l1$Date),ncol=4))
nams = names(l1)
j=1
for( i in nams){
datmat[,j] = eval(parse(text=paste("l1",i,sep="$")))
j=j+1
}
rownames(datmat)= rownames(l1[[1]])
colnames(datmat)=colnames(table1.2)
I don't care about summary statistics of Date, so just get rid of it.
datmat =datmat[,-1]
So I heard that the eval(parse(text= should be avoided as possible. I tried with the function get but didn't work.
I am just trying to start getting rid of bad programming practices, so any advice is more than welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要经历这一切。看来您正在执行 basicStats 函数已经执行的操作。这看起来太简单了,但我认为以下给出了相同的输出:
Why go through all that. it appears you're doing what the function basicStats already does. This seems too simple to be right but I think the following gives you the same output:
欢迎来到SO,我试图弄清楚你想要做什么,但除了阅读你的数据集之外,我无法得到你想要做的事情。尽管如此,我还是尝试提出一些建议。如果您编辑您的帖子并变得更精确,也许我可以提供更多帮助。
paste(l1,nams,sep="$ ")
无需循环即可工作。确实值得阅读一些更好的介绍,或者扫描更高级的资源 - 即使它们没有涵盖您的特定问题。花点时间去做这件事是非常值得的。
这里有关于交叉验证(stackexchange 的统计网站)
的相关讨论
Welcome to SO, I tried to figure out what you want to do, but other than reading your dataset I could not get what you want to do. Still though, I try to come up with some advice. Maybe I could help a little further if you'd edit your post an get a little more precise.
paste(l1,nams,sep="$")
works without the loop.lapply
and use it on our data.It's really worth the read through some of the better introductions, or also scan through more advanced ressources – even if they are not covering your particular problem. Take your time to do it it is well worth it.
Here's a related discussion on crossvalidated (stackexchange's stats site)
您可以在获得
l1
后添加此内容。更简单的方法是使用
psych
包中的describe
函数You could just add this after you get
l1
A simpler approach would be using the
describe
function in thepsych
package您可以非常轻松地将等长向量的列表放入 data.frame 中,然后只需更改名称
就可以了,就像您构造的一样。
You can get a list of equal length vectors into a data.frame very easily and then just change the names
Ends up exactly like what you constructed.