To use this, the actual database in question (e.g. There are special mechanisms to support Excel files (see above) and Microsoft Access databases, but in general any database can be accessed via its ODBC interface. ODBC data sources (and other database connections) # Remember you can also use file.choose() in place of the filename, as above.įor saving in SPSS format from R, see extensions. Very easy: library(foreign) mydata <- ame(read.spss("filename.sav" )) probably won't work out of the box, so this works well: library(gdata)ĭata <- read.xls("myexceldata.xls", sheet=1) # load the first worksheet , though you may have renamed them something more informativeīut in a Linux environment, the Windows ODBC settings etc. Indexcasedata <- sqlFetch(channel, "Sheet2") # by default Excel names individual sheets Sheet1, Sheet2. Patientdata <- sqlFetch(channel, "Vitamin_D_levels") # specify a sheet within the spreadsheet I find this easy: library(RODBC) channel <- odbcConnectExcel("Osteomalacia_data.xls") # specify the filename There are several ways to read from Excel spreadsheets. In the R Commander, you can use Data / Import data / from text file or clipboard, and, having selected a data set, Data / Active data set / Export active data set. Write.table(.) # } (read.csv and write.csv are specialized versions of read.table and write.table) Read.table(.) # } A more generic way to read/write tabular data from/to disk Write.csv(my.data, file="d:/temp/newfile.csv", row.names=FALSE) # Here's one: turn off row names to avoid creating a spurious additional column. There are several options available see the help (use ?write.csv) Write.csv(my.data, filename2) # Write the data to a new file. row.names=1 readsĪttach(my.data) # you might then want to attach the new data to the path, though this is optional # and no row names, but you can change all these defaults (e.g. # (3) The default is to assume a header row with variable names (header=TRUE), # (2) file.choose() pops up a live filename picker # Note: (1) = and <- are synonymous, and are the assignment operator (while = tests for equality) Text files (including comma-delimited value or CSV files) Postscript("mygraph.ps") # subsequent graphical output will go to a PostScript file Jpeg("mygraph.jpeg") # subsequent graphical output will go to a JPEGīmp("mygraph.bmp") # subsequent graphical output will go to a BMP Png("mygraph.png") # subsequent graphical output will go to a PNG Pdf("mygraph.pdf") # subsequent graphical output will go to a PDF Redirecting output: sink("myfile.txt") # redirect console output to a file Last <- function() cat("\n Goodbye!\n\n") First <- function() cat("\n Script ~/.Rprofile executed.\n\n") adjustWidthCallBack <- addTaskCallback(.adjustWidth) Running scripts: source("myfile.R") # load and execute a script of R commandsįor a startup script, edit ".Rprofile" in your home directory (for details see ?Startup). > dataEXCEL dataEXCEL dfEXCEL <- as.data.A few file-handling commands may be useful: setwd("c:/myfiles") # use / or \\ to separate directories under Windows (\\ becomes \ once processed through the escape character mechanism)ĭir() # list the contents of the current directory > dataSPSS dataSPSS dataStata dataStata dataSAS install.packages(“readxl”) : TRUE if R should convert variables with value labels into R factors with those levels.to.ame: TRUE if R should treat loaded data as a data frame.> dataRDS dataSPACE dataSPACE dataTAB dataCOMMA dataFW install.packages(“foreign”) The command > ls() can be used to print out all of the objects currently loaded into R. The names given to these objects when they were originally saved will be given to them when they are loaded. When R calls load(), all of the R objects saved in the file are loaded into R. Notice that the result of this function is not assigned to an object name. Setting the working directory can eliminate path confusion. When specifying the pathname, R reads forward slashes, whereas Windows reads backward slashes. getwd() will print out the current directory.setwd(“…”) will set the current working directory to a specific location.See below for instructions on how to read and load data into R from both file extensions.īefore reading any data, you must set the R working directory to the location of the data. Rdata is used to save multiple R objects, while Rds is used to save a single R object. These formats are used when R objects are saved for later use. R also has two native data formats-Rdata (sometimes shortened to Rda) and Rds. Whether the data was prepared using Excel (in CSV, XLSX, or TXT format), SAS, Stata, SPSS, or others, R can read and load the data into memory. R is capable of reading data from most formats, including files created in other statistical packages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |