#This tutorial uses the package openxlsx to convert R data frames to spreadsheets
#Although frowned on by pros, it is often useful to save objects created in R
#as spreadsheets to process in Excel

#Open Rstudio and install the package openxlsx
#This can be done by clicking the packages tab in the bottom right window of Rstudio
#and clicking on the install tab to find and install the package

#Set working directory
setwd("C:/1awinz/R_work/excel_R")
getwd()
## [1] "C:/1awinz/R_work/excel_R"
#Import data
data=read.table("data.txt", header=T)
attach(data)
names(data)
## [1] "Ord"     "Species" "Site"    "Site_Cd" "Type"    "Spec"    "Sex"    
## [8] "SL"      "CS"
#Open the package
library(openxlsx)

#Use the following function:

#write.xlsx(R_dataframe, file, asTable = FALSE, overwrite = TRUE)

#Where the two key parameters are "R_dataframe" and "file"
#"R dataframe" is the name of the R dataframe that you want to convert to an Excel file
#For example a group summary file created from a raw data file
#It should be active in your R enviornment
#"file" is the name and directory path for the file that you want to create
#The defaults for "asTable = FALSE", "overwrite = TRUE", should be fine.

#Here is an example from my work:
write.xlsx(data, "C:/1awinz/R_work/excel_R/data_test.xlsx", asTable = FALSE, overwrite = TRUE)

#Happy converting!