R Program Having Package for Sas7bdat Upload File

How to Export Data from R

In this tutorial, we volition larn how to consign information from R surround to dissimilar formats.

To export data to the difficult drive, you demand the file path and an extension. First of all, the path is the location where the information volition be stored. In this tutorial, you will run across how to store data on:

  • The hard drive
  • Google Drive
  • Dropbox

Secondly, R allows the users to export the data into dissimilar types of files. We comprehend the essential file'southward extension:

  • csv
  • xlsx
  • RDS
  • SAS
  • SPSS
  • STATA

Overall, it is not difficult to export data from R.

In this tutorial, you lot will learn-

  • Export to Hard drive
  • How to Export a DataFrame to a CSV File in R
  • How to Consign a Data from R to Excel File
  • Exporting Data from R to Different Software
  • Exporting Data from R to SAS File
  • How to Export Data from R to STATA File
  • Collaborate with the Deject Services
  • Google Bulldoze
  • Consign to Dropbox

Export to Hard drive

To begin with, you tin salvage the information directly into the working directory. The post-obit code prints the path of your working directory:

directory <-getwd() directory

Output:

## [1] "/Users/15_Export_to_do"

By default, the file volition exist saved in the below path.

For Mac OS:

/Users/USERNAME/Downloads/

For Windows:

C:\Users\USERNAME\Documents\

You can, of course, set a different path. For instance, you can change the path to the download folder.

Create data frame

Start of all, let's import the mtcars dataset and become the mean of mpg and disp grouped by gear.

library(dplyr) df <-mtcars % > %     select(mpg, disp, gear) % > %     group_by(gear) % > %     summarize(mean_mpg = mean(mpg), mean_disp = hateful(disp)) df          

Output::

## # A tibble: iii x 3 ##	gear mean_mpg mean_disp ##	<dbl>	<dbl>	lt;dbl> ## 1	3 16.10667  326.3000 ## 2 	4 24.53333  123.0167 ## 3	v 21.38000  202.4800

The tabular array contains three rows and three columns. You can create a CSV file with the role write.csv in R.

How to Export a DataFrame to a CSV File in R

The bones syntax of write.csv in R to Export the DataFrame to CSV in R:

write.csv(df, path) arguments -df: Dataset to salve. Demand to be the same proper noun of the information frame in the surround. -path: A string. Set up the destination path. Path + filename + extension i.east. "/Users/USERNAME/Downloads/mydata.csv" or the filename + extension if the folder is the same as the working directory

Instance:

write.csv(df, "table_car.csv")

Code Explanation

  • write.csv(df, "table_car.csv"): Create a CSV file in the hard drive:
    • df: proper noun of the information frame in the environs
    • "table_car.csv": Proper noun the file table_car and store information technology equally csv

Note: You tin can apply the function write.csv in R as write.csv2() to divide the rows with a semicolon for R export to csv data.

write.csv2(df, "table_car.csv")

Note: For pedagogical purpose only, we created a function called open_folder() to open the directory binder for y'all. Y'all just need to run the code below and come across where the csv file is stored. You should see a file names table_car.csv for information R export to csv.

# Run this code to create the function open_folder <-office(dir){ 	if (.Platform['Bone.type'] == "windows"){ 	shell.exec(dir)   	} else { 	system(paste(Sys.getenv("R_BROWSER"), dir))   } } # Telephone call the function to open the folder open_folder(directory)

How to Consign a Data from R to Excel File

At present, we will acquire how to export data from R to Excel:

Export data from R to Excel is trivial for Windows users and trickier for Mac OS user. Both users volition use the library xlsx to create an Excel file. The slight difference comes from the installation of the library. Indeed, the library xlsx uses Java to create the file. Coffee needs to be installed if not present in your car for Information R consign to Excel.

Windows users

If yous are a Windows user, you can install the library directly with conda to export dataframe to excel R:

conda install -c r r-xlsx

Once the library installed, you can apply the function write.xlsx(). A new Excel workbook is created in the working directory for R export to Excel information

library(xlsx) write.xlsx(df, "table_car.xlsx")

If you lot are a Mac Bone user, you need to follow these steps:

  • Step one: Install the latest version of Java
  • Footstep 2: Install library rJava
  • Stride three: Install library xlsx

Step 1) You lot could download Java from official Oracle site and install it.

Y'all tin can go back to Rstudio and bank check which version of Java is installed.

system("java -version")

At the fourth dimension of the tutorial, the latest version of Coffee is 9.0.4.

Step ii) Y'all need to install rjava in R. We recommended you lot to install R and Rstudio with Anaconda. Anaconda manages the dependencies betwixt libraries. In this sense, Anaconda will handle the intricacies of rJava installation.

First of all, you need to update conda so install the library. You can copy and paste the side by side two lines of code in the terminal.

conda - conda update conda install -c r r-rjava            

Adjacent, open rjava in Rstudio

library(rJava)

Stride three) Finally, it is time to install xlsx. Over again, you can utilise conda to practice it:

conda install -c r r-xlsx

Just as the windows users, y'all tin save data with the function write.xlsx()

library(xlsx)

Output:

## Loading required package: xlsxjars
write.xlsx(df, "table_car.xlsx")

Exporting Data from R to Different Software

Exporting data to different software is as elementary as importing them. The library "haven" provides a user-friendly style to export information to

  • spss
  • sas
  • stata

Kickoff of all, import the library. If you lot don't have "haven", you can get hither to install it.

library(haven)

SPSS file

Below is the code to export the data to SPSS software:

write_sav(df, "table_car.sav")

Exporting Data from R to SAS File

Just as unproblematic equally spss, you can export to sas

write_sas(df, "table_car.sas7bdat")

How to Export Data from R to STATA File

Finally, haven library allows writing .dta file.

write_dta(df, "table_car.dta")

R

If you want to save a information frame or whatever other R object, you can use the save() function.

save(df, file ='table_car.RData')

You tin cheque the files created higher up in the present working directory

Interact with the Deject Services

Last simply not least, R is equipped with fantastic libraries to interact with the cloud computing services. The last role of this tutorial deals with export/import files from:

  • Google Drive
  • Dropbox

Notation: This office of the tutorial assumes you have an account with Google and Dropbox. If not, you can quickly create 1 for – Google Bulldoze: https://accounts.google.com/SignUp?hl=en – Dropbox: https://www.dropbox.com/h

Google Drive

You need to install the library googledrive to access the part assuasive to interact with Google Drive.

The library is non yet bachelor at Anaconda. Yous tin can install it with the code below in the console.

install.packages("googledrive")

and you open the library.

library(googledrive)

For non-conda user, installing a library is like shooting fish in a barrel, you can use the part install.packages('Proper noun OF Package) with the name of the package within the parenthesis. Don't forget the ' '. Notation that, R is supposed to install the package in the `libPaths() automatically. Information technology is worth to see it in action.

Upload to Google Drive

To upload a file to Google bulldoze, you need to use the function drive_upload().

Each time yous restart Rstudio, you will be prompted to permit access tidyverse to Google Drive.

The basic syntax of drive_upload() is

drive_upload(file, path = Null, name = NULL) arguments: - file: Total proper noun of the file to upload (i.e., including the extension) - path: Location of the file- name: You can rename it as you lot wish. Past default, it is the local proper noun.

After you launch the code, you need to confirm several questions

drive_upload%<("table_car.csv", proper name ="table_car")

Output:

## Local file:  ## * table_car.csv  ## uploaded into Drive file:  ## * table_car: 1hwb57eT-9qSgDHt9CrVt5Ht7RHogQaMk  ## with MIME type:  ## * text/csv

Y'all type 1 in the panel to confirm the access

Then, y'all are redirected to Google API to allow the access. Click Allow.

In one case the authentication is complete, you lot tin can quit your browser.

In the Rstudio'south panel, you tin see the summary of the step done. Google successfully uploaded the file located locally on the Drive. Google assigned an ID to each file in the bulldoze.

You tin can see this file in Google Spreadsheet.

drive_browse("table_car")

Output:

You lot will be redirected to Google Spreadsheet

Import from Google Drive

Upload a file from Google Bulldoze with the ID is convenient. If you know the file name, you can get its ID as follow:

Note: Depending on your internet connection and the size of your Drive, it takes times.

x <-drive_get("table_car") as_id(10)

You stored the ID in the variable x. The function drive_download() allows downloading a file from Google Drive.

The basic syntax is:

drive_download(file, path = Aught, overwrite = FALSE) arguments: - file:  Proper name or id of the file to download -path: Location to download the file. By default, information technology is downloaded to the working directory and the name as in Google Bulldoze -overwrite = FALSE: If the file already exists, don't overwrite it. If set to TRUE, the old file is erased and replaced by the new one.

You lot tin finally download the file:

download_google & lt; - drive_download(as_id(x), overwrite = True)

Code Explanation

  • drive_download(): Function to download a file from Google Drive
  • as_id(10): Use the ID to browse the file in Google Drive
  • overwrite = True: If file exists, overwrite it, else execution halted To see the name of the file locally, you can apply:

Output:

The file is stored in your working directory. Call back, you need to add the extenstion of the file to open it in R. You tin create the full name with the function paste() (i.e. table_car.csv)

google_file <-download_google$local_path google_file path <-paste(google_file, ".csv", sep = "") google_table_car <-read.csv(path) google_table_car

Output:

##   10 gear mean_mpg mean_disp ## 1 1    3 sixteen.10667  326.3000 ## ii ii    4 24.53333  123.0167 ## 3 three    five 21.38000  202.4800          

Finally, you lot can remove the file from your Google drive.

## remove file drive_find("table_car") %>%drive_rm()

Output:

It's a dull process. Takes time to delete

Consign to Dropbox

R interacts with Dropbox via the rdrop2 library. The library is not bachelor at Anaconda also. You can install it via the console

install.packages('rdrop2')
library(rdrop2)

You need to provide temporary admission to Dropbox with your credential. After the identification is washed, R tin create, remove upload and download to your Dropbox.

First of all, you need to give access to your business relationship. The credentials are buried during all session.

drop_auth()

You will be redirected to Dropbox to confirm the authentication.

You will get a confirmation page. Yous can shut information technology and return to R

You can create a folder with the part drop_create().

  • drop_create('my_first_drop'): Create a folder in the first branch of Dropbox
  • drop_create('First_branch/my_first_drop'): Create a folder inside the existing First_branch folder.
drop_create('my_first_drop')

Output:

In DropBox

To upload the .csv file into your Dropbox, use the part drop_upload().

Basic syntax:

drop_upload(file, path = NULL, mode = "overwrite") arguments: - file: local path - path: Path on Dropbox  - mode = "overwrite":  By default, overwrite an existing file. If gear up to `add`, the upload is not completed.          
drop_upload('table_car.csv', path = "my_first_drop")

Output:

At DropBox

You lot tin read the csv file from Dropbox with the office drop_read_csv()

dropbox_table_car <-drop_read_csv("my_first_drop/table_car.csv") dropbox_table_car

Output:

##   X gear mean_mpg mean_disp ## 1 1    3 sixteen.10667  326.3000 ## ii 2    4 24.53333  123.0167 ## 3 three    5 21.38000  202.4800          

When you are washed using the file and desire to delete it. Yous need to write the path of the file in the function drop_delete()

drop_delete('my_first_drop/table_car.csv')

Output:

It is also possible to delete a folder

drop_delete('my_first_drop')

Output:

Summary

Nosotros tin can summarize all the functions in the table below

Library Objective Function
base of operations Export csv write.csv()
xlsx Export excel write.xlsx()
oasis Export spss write_sav()
haven Consign sas write_sas()
haven Export stata write_dta()
base Export R salvage()
googledrive Upload Google Drive drive_upload()
googledrive Open up in Google Drive drive_browse()
googledrive Call back file ID drive_get(as_id())
googledrive Dowload from Google Bulldoze download_google()
googledrive Remove file from Google Bulldoze drive_rm()
rdrop2 Authentification drop_auth()
rdrop2 Create a folder drop_create()
rdrop2 Upload to Dropbox drop_upload()
rdrop2 Read csv from Dropbox drop_read_csv
rdrop2 Delete file from Dropbox drop_delete()

jamesstaread.blogspot.com

Source: https://www.guru99.com/r-exporting-data.html

0 Response to "R Program Having Package for Sas7bdat Upload File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel