1

How can I save data from an Excel sheet to .RData file in R? I want to use one of the packages in R and to load my dataset as data(dataset) i think i have to save the data as .RData file and then load that into the package. My data currently is in an Excel spreadsheet.

my excel sheets has column names like x, y , time.lag. I have saved it as .csv then i use: x=read.csv('filepath', header=T,) then i say data(x) and it shows dataset 'x' not found

Maddy
  • 363
  • 3
  • 6
  • 16

3 Answers3

6

There are also several packages that allow directly reading from XLS and XLSX files. We've even had a question on that topic here and here for example. However you decide to read in the data, saving into an RData can be handled with save, save.image, saveRDS and probably some others I'm not thinking about.

Community
  • 1
  • 1
Chase
  • 67,710
  • 18
  • 144
  • 161
5

save your Excel data as a .csv file and import it using read.csv() or read.table(). Help on each will explain the options.

For example, you have a file called myFile.xls, save it as myFile.csv.

library(BBMM)

# load an example dataset from BBMM
data(locations)


# from the BBMM help file
BBMM <- brownian.bridge(x=locations$x, y=locations$y, time.lag=locations$time.lag[-1], location.error=20,  cell.size=50)
bbmm.summary(BBMM)

# output of summary(BBMM)
Brownian motion variance :  3003.392
Size of grid :  138552 cells
Grid cell size :  50


# subsitute locations for myData for your dataset that you have read form a myFile.csv file
myData <- read.csv(file='myFile.csv', header=TRUE)

head(myData) # will show the first 5 entries in you imported data

# use whatever you need from the BBMM package now ....
John
  • 41,131
  • 31
  • 82
  • 106
  • so the package i want to use is BBMM, it has certain datasets loaded into it already,so i have saved my dataset as a .csv file and i dont know hoe to load it so that i can use that package. – Maddy Jun 22 '12 at 06:23
  • also @Maddy: http://cran.r-project.org/web/packages/BBMM/BBMM.pdf when your data is loaded (in this case as a data frame) you then use the functionality of the BBMM package on your loaded data. I do not know enough about the package but you may wish to consider which of the variables in your data that you need to extract as object classes for the package. Run through the examples in the package from the link above. – John Jun 22 '12 at 08:02
  • my data is in excel! i want to load it into this package! i did try saving it as .csv file but when i do data(x) where x is my data saved as x.csv, it says dataset x.ccsv not found! – Maddy Jun 27 '12 at 05:11
  • yes i am aware of that and i did that, but still it says data set x not found. – Maddy Jun 27 '12 at 05:47
  • @John: basically i want to use a package in R on my own dataset. the help for that package shows an example where it loads the package using data(dataset name). but this dataset is inbuilt in the package. so basically i want to know how i can load my own dataset so that i can use that package on that. the package is called BBMM in R. – Maddy Jun 27 '12 at 07:29
  • @john: so i have done that already! but after that step, what command i need to give in order use the package BBMM on myFile.csv? As in when it does the operations on dataset locations, it says data(locations) and then gives the command BBMM<-......so and so. Now can i just write BBMM<-brownian.bridge(x=locations$x, y=locations$y, time.lag=locations$time.lag[-1], location.error=20, cell.size=50) bbmm.summary(BBMM) so that it does this on myFile.csv? – Maddy Jun 27 '12 at 12:30
4

Check RODBC package. You can find an example in R Data Import/Export. You can query data from excel sheet as if from a database table.

The benefit of reading Excel sheet with RODBC is that you get dates (if you work with any) in a proper format. With intermediate CSV, you'd need to specify a column type, unless you want it to be a factor or string. Also you can query only a portion of your data if you need so thus making subset() unnecessary.

mlt
  • 1,595
  • 2
  • 21
  • 52