Difference between revisions of "R: Export"

From OnnoWiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 33: Line 33:
 
             sheetName="MTCARS", append=TRUE)
 
             sheetName="MTCARS", append=TRUE)
  
 +
==R Object==
 +
 +
# Save a single object to a file
 +
saveRDS(mtcars, "mtcars.rds")
 +
# Restore it under a different name
 +
my_data <- readRDS("mtcars.rds")
 +
 +
==R Workspace==
 +
 +
# Save your workspace
 +
save.image(file = "my_work_space.RData")
 +
# Load the workspace again
 +
load("my_work_space.RData")
 +
 +
==Referensi==
 +
 +
* http://www.sthda.com/english/wiki/exporting-data-from-r
  
  

Latest revision as of 10:34, 28 November 2019

# Loading mtcars data
data("mtcars")
# Write data to txt file: tab separated values
# sep = "\t"
write.table(mtcars, file = "mtcars.txt", sep = "\t",
            row.names = TRUE, col.names = NA)
# Write data to csv files:  
# decimal point = "." and value separators = comma (",")
write.csv(mtcars, file = "mtcars.csv")
# Write data to csv files: 
# decimal point = comma (",") and value separators = semicolon (";")
write.csv2(mtcars, file = "mtcars.csv")


# Loading mtcars data
data("mtcars")
library("readr")
# Writing mtcars data to a tsv file
write_tsv(mtcars, path = "mtcars.txt")
# Writing mtcars data to a csv file
write_csv(mtcars, path = "mtcars.csv")


Excel

library("xlsx")
# Write the first data set in a new workbook
write.xlsx(USArrests, file = "myworkbook.xlsx",
      sheetName = "USA-ARRESTS", append = FALSE)
# Add a second data set in a new worksheet
write.xlsx(mtcars, file = "myworkbook.xlsx", 
           sheetName="MTCARS", append=TRUE)

R Object

# Save a single object to a file
saveRDS(mtcars, "mtcars.rds")
# Restore it under a different name
my_data <- readRDS("mtcars.rds")

R Workspace

# Save your workspace
save.image(file = "my_work_space.RData")
# Load the workspace again
load("my_work_space.RData")

Referensi


Pranala Menarik