[英]How do I export all variables from a project to .csv so that I can then import them into a Python environment?
I have a large project in RStudio that I would like to attempt some things with in Python and want to know if there is a way of effectively packaging up all of the environment variables (data frames, lists, atomic vectors, etc.) and importing those into Spyder (Python). 我在RStudio中有一个大型项目,我想在Python中尝试一些操作,并且想知道是否存在一种有效包装所有环境变量(数据框,列表,原子向量等)并导入的方法。将它们导入Spyder(Python)。
If not directly possible, I am aware that I can read files into Python in numerous ways, and as such am wondering if there is a method I could use to iterate through my R environment and save all of the variables as .csv files so that I can later read them into Spyder? 如果不是直接可行的话,我知道我可以通过多种方式将文件读入Python,因此我想知道是否存在一种可用于迭代R环境并将所有变量另存为.csv文件的方法,以便我以后可以将它们读入Spyder吗?
I have tried this code but to no avail: 我已经尝试过此代码,但无济于事:
files <- mget(ls())
for (i in 1:length(files)){
write.csv(files[[i]], paste(names(files[i]), ".csv", sep = ""))
}
I would recommend looking at some of the packages for transferring data, but to specifically answer your question: 我建议您查看一些用于传输数据的软件包,但要专门回答您的问题:
x1 <- "hello"
x2 <- "these are some"
x3 <- "objects"
for (i in names(.GlobalEnv)) {
write.csv(.GlobalEnv[[i]], file = sprintf("%s.csv", i))
}
You could more explicitly export the objects by storing the objects for export in a separate environment, too. 您也可以通过将要存储的对象存储在单独的环境中来更明确地导出对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.