在r中将数据放在new.env中有什么好处? [英] What are the advantages of placing data in a new.env in r?

查看:966
本文介绍了在r中将数据放在new.env中有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于诸如时间序列之类的数据,是一个新的.env类似于数据库?



我的问题主要是从R中下载资产价格,建议将其放入新的.env。为什么这样呢谢谢:

 库(TTR)

url = paste('http: nasdaq.com/markets/indices/nasdaq-100.aspx',sep=)
txt = join(readLines(url))

#从这个页面提取表
temp = extract.table.from.webpage(txt,'Symbol',hasHeader = T)
temp [,2]

#符号
symbols = c(temp [ ,2])[2:101]

货币(USD)
股票(符号,货币=美元,乘数= 1)

#创建新的环境来存储符号
symEnv< - new.env()

#getSymbols并将符号分配给symEnv环境
getSymbols(symbols,from ='2002- 09-01',to ='2013-10-17',env = symEnv)


解决方案

如果您的数据较大,您必须通过传递函数进行修改,这样才有优势。当您向修改它们的函数发送 data.frame s或 vector s时,R将创建一个数据副本在更改之前。然后,您将从函数返回修改的数据,并覆盖旧数据以完成修改步骤。



如果您的数据很大,复制每个函数调用的数据可能会导致不必要的开销。使用环境提供了一个解决这个开销的方法。 环境由功能处理不同。如果您将环境传递给函数并修改内容,则R将直接在环境上进行操作,而无需复制的。因此,将数据放在环境中,并将环境传递给函数,而不是直接传递数据,您可以避免复制大数据集。

 #here我在环境中创建一个data.frame并传递环境
#到一个修改数据的功能。
e < - new.env()
e $ k < - data.frame(a = 1:3)
f< - function(e){e $ k [1,1 ]< - 10}
f(e)
#您可以看到原始数据已更改。
e $ k
a
1 10
2 2
3 3

#或者,如果我只传递了data.frame,操作不要影响
#原始数据。
k< - data.frame(a = 1:3)
f2< - function(k){k [1,1] < - 10}
f2(k)
k
a
1 1
2 2
3 3


What are the advantages of placing data in a new .env in R?-speed, etc.

For data such as time series, is an new .env analogous to a database?

My question spans initally from downloading asset prices in R where it was suggested to place them into a new .env. Why is this so? Thank you:

library(TTR)

url = paste('http://www.nasdaq.com/markets/indices/nasdaq-100.aspx',sep="")
 txt = join(readLines(url)) 

 # extract tables from this pages
 temp = extract.table.from.webpage(txt, 'Symbol', hasHeader = T)
 temp[,2]

 # Symbols
 symbols = c(temp[,2])[2:101]

 currency("USD")
stock(symbols, currency = "USD", multiplier = 1)

# create new environment to store symbols
symEnv <- new.env()

# getSymbols and assign the symbols to the symEnv environment
getSymbols(symbols, from = '2002-09-01', to = '2013-10-17', env = symEnv)

解决方案

There are advantages to this if your data is large and you have to modify it by passing it through functions. When you send data.frames or vectors to functions that modify them, R will make a copy of the data before making changes to it. You'd then return the modified data from the function and overwrite the old data to complete the modification step.

If your data is large, copying the data for each function call may result in an undesirable amount of overhead. Using environments provides a way around this overhead. environments are handled differently by functions. If you pass an environment to a function and modify the contents, R will operate directly on the environment without making a copy of it. So by putting your data in an environment and passing the environment to the function instead of directly passing the data, you can avoid copying the large dataset.

# here I create a data.frame inside an environment and pass the environment
# to a function that modifies the data.
e <- new.env()
e$k <- data.frame(a=1:3)
f <- function(e) {e$k[1,1] <- 10}
f(e)
# you can see that the original data was changed.
e$k
   a
1 10
2  2
3  3

# alternatively, if I pass just the data.frame, the manipulations do not affect the 
# original data.
k <- data.frame(a=1:3)
f2 <- function(k) {k[1,1] <- 10}
f2(k)
k
  a
1 1
2 2
3 3

这篇关于在r中将数据放在new.env中有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆