简体   繁体   English

如何查看 .RData 文件中的数据?

[英]How to see data from .RData file?

I saw some similar qestions and I tried to work it out on my own, but I couldn't.我看到了一些类似的问题,我试图自己解决,但我做不到。 This is my problem:这是我的问题:

I have to load a isfar.RData file to use it in other computation (which are not important to describe here).我必须加载一个 isfar.RData 文件才能在其他计算中使用它(这里不重要描述)。 And I would like to simply see how looks data in this isfar.RData file eg what numbers, columns, rows it carries.我想简单地看看这个 isfar.RData 文件中的数据看起来如何,例如它携带的数字、列、行。

First I load my file:首先我加载我的文件:

isfar<-load("C:/Users/isfar.RData") 

When I try to obtain this information (I'm using Rcmdr) by ls() function or marking isfar at the beginning after loading I get in the output window: [1] "isfar" instead of the table.当我尝试通过 ls() 函数或在加载后在开头标记 isfar 获取此信息(我正在使用 Rcmdr)时,我进入输出窗口:[1] “isfar”而不是表格。 Why?为什么?

Thanks a lot, I appreciate all of the answers!非常感谢,我感谢所有的答案! Hope it's comprehensible what I wrote, Im not a native speaker.希望我写的可以理解,我不是母语人士。

I think the problem is that you load isfar data.frame but you overwrite it by value returned by load .我认为问题是,你load isfar data.frame但你通过返回的值覆盖它load

Try either:尝试:

load("C:/Users/isfar.RData") 
head(isfar)

Or more general way或更一般的方式

load("C:/Users/isfar.RData", ex <- new.env())
ls.str(ex) 

you can try你可以试试

isfar <- get(load('c:/users/isfar.Rdata')) isfar <- get(load('c:/users/isfar.Rdata'))

this will assign the variable in isfar.Rdata to isfar .这会将 isfar.Rdata 中的变量分配给 isfar 。 After this assignment, you can use str(isfar) or ls(isfar) or head(isfar) to get a rough look of the isfar.在此赋值之后,您可以使用 str(isfar) 或 ls(isfar) 或 head(isfar) 来粗略查看 isfar。

Look at the help page for load .查看load的帮助页面。 What load returns is the names of the objects created, so you can look at the contents of isfar to see what objects were created. load 返回的是创建的对象的名称,所以你可以查看 isfar 的内容,看看创建了哪些对象。 The fact that nothing else is showing up with ls() would indicate that maybe there was nothing stored in your file. ls()没有显示任何其他内容的事实表明,您的文件中可能没有存储任何内容。

Also note that load will overwrite anything in your global environment that has the same name as something in the file being loaded when used with default behavior.另请注意,当与默认行为一起使用时, load 将覆盖全局环境中与正在加载的文件中名称相同的任何内容。 If you mainly want to examine what is in the file, and possibly use something from that file along with other objects in your global environment then it may be better to use the attach function or create a new environment ( new.env ) and load the file into that environment using the envir argument to load .如果您主要想检查文件中的内容,并可能将该文件中的某些内容与全局环境中的其他对象一起使用,那么最好使用attach函数或创建一个新环境 ( new.env ) 并加载使用envir参数load到该环境中。

This may fit better as a comment but I don't have enough reputation, so I put it here.这可能更适合作为评论,但我没有足够的声誉,所以我把它放在这里。
It worth mentioning that the load() function will retain the object name that was originally saved no matter how you name the .Rdata file.值得一提的是无论您如何命名.Rdata文件, load()函数都会保留最初保存的对象名称

Please check the name of the data.frame object used in the save() function .请检查save()函数中使用的 data.frame 对象的名称 If you were using RStudio, you could check the upper right panel, Global Environment-Data, to find the name of the data you load.如果您使用的是 RStudio,您可以检查右上角的面板 Global Environment-Data,以找到您加载的数据的名称。

If you have a lot of variables in your Rdata file and don't want them to clutter your global environment, create a new environment and load all of the data to this new environment.如果您的Rdata文件中有很多变量并且不希望它们干扰您的全局环境,请创建一个新环境并将所有数据加载到这个新环境中。

load(file.path("C:/Users/isfar.RData"), isfar_env <- new.env() )

# Access individual variables in the RData file using '$' operator
isfar_env$var_name 

# List all of the variable names in RData:
ls(isfar_env)

You can also import the data via the "Import Dataset" tab in RStudio, under "global environment."您还可以通过 RStudio 中“全局环境”下的“导入数据集”选项卡导入数据。 Use the text data option in the drop down list and select your .RData file from the folder.使用下拉列表中的文本数据选项并从文件夹中选择您的 .RData 文件。 Once the import is complete, it will display the data in the console.导入完成后,它将在控制台中显示数据。 Hope this helps.希望这可以帮助。

It sounds like the only varaible stored in the .RData file was one named isfar .这听起来像存储在唯一varaible .RData文件是一个名为isfar

Are you really sure that you saved the table?你真的确定你保存了表格吗? The command should have been:命令应该是:

save(the_table, file = "isfar.RData")

There are many ways to examine a variable.有很多方法可以检查变量。

Type it's name at the command prompt to see it printed.在命令提示符下键入它的名称以查看它的打印。 Then look at str , ls.str , summary , View and unclass .然后看看strls.strsummaryViewunclass

num <- seq(0, 5, length.out=10) #create object num
num
[1] 0.00 1.25 2.50 3.75 5.00
save(num, file = 'num.RData') #save num ro RData
rm(num) #remove num 
load("num.RData") #load num from RData
num
[1] 0.00 1.25 2.50 3.75 5.00

> isfar<-load("num.RData")
> typeof(isfar)
 [1] "character"
> isfar  #list objects saved in RData
 [1] "num"

You don't seem to need to assign it to a variable.您似乎不需要将其分配给变量。 That bit magically happens...那一点神奇地发生了......

get(load('C:/Users/isfar.Rdata'))

Or if it's in the same folder as your R code...或者,如果它与您的 R 代码在同一文件夹中...

get(load('isfar.Rdata'))
isfar<-load("C:/Users/isfar.RData") 
if(is.data.frame(isfar)){
   names(isfar)
}

If isfar is a dataframe, this will print out the names of its columns.如果 isfar 是一个数据框,这将打印出其列的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM