How to see data from .RData file?
Asked Answered
C

10

87

I have to load a isfar.RData file to use it in other computation (which are not important to describe here). And I would like to simply see how looks data in this isfar.RData file e.g. what numbers, columns, rows it carries.

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. Why?

Thanks a lot, I appreciate all of the answers! Hope it's comprehensible what I wrote, Im not a native speaker.

Chafee answered 1/9, 2011 at 12:50 Comment(1)
If you would like to save/load a single R object, then look at the alternative approach using the complementary functions saveRDS() and readRDS.Interrogate
S
94

I think the problem is that you load isfar data.frame but you overwrite it by value returned by 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) 
Sere answered 1/9, 2011 at 20:14 Comment(2)
your more general approach works well! However, can you please more explain why using ex<- new.env()? Does not this creates a new environment? My aim is to include .RData within the project in GitLab and than allow everyone to access the data. But, would not you approach create a new environment instead? Thanks!Clementina
@Clementina Yes, it creates new environment, so it does not overwrite anything. If you want to share the data then better use saveRDS, readRDS (or from readr package write_rds, read_rds). Then instead of load you assign them to an object (like read.csv): x <- readRDS("some_file.rds")Sere
R
45

you can try

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

this will assign the variable in isfar.Rdata to isfar . After this assignment, you can use str(isfar) or ls(isfar) or head(isfar) to get a rough look of the isfar.

Rubstone answered 23/8, 2016 at 4:48 Comment(3)
get(load('isfar.Rdata')) seems to create the variable even without the assignment isfar <- bitMindexpanding
get(load('xxx.Rdata')) will retrieve the variable stored inside the Rdata file, and the variable name is the same name when you saved the Rdata file,e.g.,save(yyy,file = xxx.Rdata'), the variable name might not be the same as the filename. I suggest using the assignment because you may get lost when there is too many variables inside your workspace.Rubstone
you end up with two variables with the same contents if you assign it to something with a different name and this adds to having too many variables inside your workspaceMindexpanding
W
10

Look at the help page for 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. The fact that nothing else is showing up with ls() would indicate that maybe there was nothing stored in your file.

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. 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.

Widener answered 1/9, 2011 at 16:22 Comment(0)
Q
9

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.

Please check the name of the data.frame object used in the save() function. If you were using RStudio, you could check the upper right panel, Global Environment-Data, to find the name of the data you load.

Questa answered 31/7, 2018 at 20:27 Comment(0)
V
6

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.

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)
Vaulting answered 18/2, 2020 at 20:31 Comment(0)
V
2

You can also import the data via the "Import Dataset" tab in RStudio, under "global environment." Use the text data option in the drop down list and select your .RData file from the folder. Once the import is complete, it will display the data in the console. Hope this helps.

Valerivaleria answered 30/9, 2019 at 21:58 Comment(0)
G
1

It sounds like the only varaible stored in the .RData file was one named 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.

Goda answered 1/9, 2011 at 12:55 Comment(1)
I'm not exactly sure what was saved in that file, I assume it was a table. But even if it was one variable saved, how can I see how it looks (numbers)?Chafee
M
1

You don't seem to need to assign it to a variable. That bit magically happens. In fact, assigning it to a variable might mean you end up with two variables with the same data.

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

Or if it's in the same folder as your R code...

get(load('isfar.Rdata'))
Mindexpanding answered 26/7, 2021 at 4:29 Comment(0)
A
0
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.

Accouter answered 1/9, 2011 at 13:4 Comment(0)
S
0
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"
Subdominant answered 9/6, 2021 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.