从列表中获取 data.frame 并从字符串中调用对象

发布于 2024-12-12 18:58:08 字数 730 浏览 0 评论 0原文

我想复制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑾兮 2024-12-19 18:58:08

为什么要经历这一切。看来您正在执行 basicStats 函数已经执行的操作。这看起来太简单了,但我认为以下给出了相同的输出:

basicStats(table1.2)[,-1]

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:

basicStats(table1.2)[,-1]
月竹挽风 2024-12-19 18:58:08

欢迎来到SO,我试图弄清楚你想要做什么,但除了阅读你的数据集之外,我无法得到你想要做的事情。尽管如此,我还是尝试提出一些建议。如果您编辑您的帖子并变得更精确,也许我可以提供更多帮助。

  1. 分配变量时使用 <- 而不是 =。这是 R 的方法。
  2. 如果你还没有使用 RStudio ,去获取它,它会对你有很大帮助,因为它有很好的自动完成功能,以及良好的帮助集成。 (你可以在rstudio中使用alt+ - 来获取<-,以帮助你习惯它)
  3. R是一种矢量化语言,通常你不需要循环,例如: paste(l1,nams,sep="$ ") 无需循环即可工作。
  4. 您已经在使用 lapply,这很好。通常,编写一个函数然后将这个函数与 lapply 结合起来并将其用于我们的数据通常效果很好。
  5. 确实值得阅读一些更好的介绍,或者扫描更高级的资源 - 即使它们没有涵盖您的特定问题。花点时间去做这件事是非常值得的。

    这里有关于交叉验证(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.

  1. Use <- instead of = when assigning variables. It's the R way to do it.
  2. If you do not use RStudio already, go an get it, it will help you a lot, since it has nice autocomplete, and good help integration. (you can use alt+ - to get <- in rstudio, to help you get used to it)
  3. R is a vectorized language, often you do not need loops, E.g.: paste(l1,nams,sep="$") works without the loop.
  4. You are already using lapply, that's nice. Often it works well to write a function and then combine this own function with lapply and use it on our data.
  5. 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)

迷爱 2024-12-19 18:58:08

您可以在获得 l1 后添加此内容。

datmat  <- setNames(as.data.frame(l1), names(l1))

更简单的方法是使用 psych 包中的 describe 函数

psych::describe(table1.2)

You could just add this after you get l1

datmat  <- setNames(as.data.frame(l1), names(l1))

A simpler approach would be using the describe function in the psych package

psych::describe(table1.2)
岁月静好 2024-12-19 18:58:08

您可以非常轻松地将等长向量的列表放入 data.frame 中,然后只需更改名称

> df.stats <- as.data.frame(lapply(table1.2,basicStats)[-1])
Warning message:
In sum(X) : Integer overflow - use sum(as.numeric(.))
> names(df.stats) <- names(table1.2)[-1]
> str(df.stats)
'data.frame':   16 obs. of  4 variables:
 $ rtn   : num  9.84e+03 0.00 -2.30e-01 1.32e-01 -8.57e-03 ...
 $ vwretd: num  9.84e+03 0.00 -1.71e-01 1.15e-01 -4.25e-03 ...
 $ ewretd: num  9.84e+03 0.00 -1.04e-01 1.07e-01 -2.57e-03 ...
 $ sprtr: num  9.84e+03 0.00 -2.05e-01 1.16e-01 -4.87e-03 ...

就可以了,就像您构造的一样。

You can get a list of equal length vectors into a data.frame very easily and then just change the names

> df.stats <- as.data.frame(lapply(table1.2,basicStats)[-1])
Warning message:
In sum(X) : Integer overflow - use sum(as.numeric(.))
> names(df.stats) <- names(table1.2)[-1]
> str(df.stats)
'data.frame':   16 obs. of  4 variables:
 $ rtn   : num  9.84e+03 0.00 -2.30e-01 1.32e-01 -8.57e-03 ...
 $ vwretd: num  9.84e+03 0.00 -1.71e-01 1.15e-01 -4.25e-03 ...
 $ ewretd: num  9.84e+03 0.00 -1.04e-01 1.07e-01 -2.57e-03 ...
 $ sprtr: num  9.84e+03 0.00 -2.05e-01 1.16e-01 -4.87e-03 ...

Ends up exactly like what you constructed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文