Analysis in R: Various files can be read and exported. The “rio” package

RAnalytics
スポンサーリンク

Reading data from SAS, Excel, CSV, or the clipboard into R and exporting it to the desired file format is a bit tedious, requiring different commands and packages for each format.

Here is the “rio package” which solves such problems.

It is a beginner-friendly design that supports reading and exporting of many file formats and does not require complicated settings.To export, simply set the desired file extension.

In most cases, the use of import and export will be sufficient.

Package version is 0.5.29. Checked with R version 4.2.2.

スポンサーリンク

Supported File Formats

Here are some typical readings and exports.

File ExtensionReadWrite
tsv
csv
psv
fwf
rds (R)
Rdata (R)
json
dta (Stata)
sav (SPSS)
dbd (XBASE)
xls, xlsx (Excel)Only xlsx file
arft
R (R)
xml
sas7bdat, xpt (SAS)×
mtp×
rec×
syd (Systat)×
dif×
ods×
Fortran×
Clipbord×

Install Package

Run the following command.

#Install Package
install.packages("rio")

Example

Since commands alone are not beginner friendly, we use the tcltk package so that data can be read and exported from the GUI.

#Command to specify files to be read using GUI
library("tcltk")
library("rio")
SelectData <- paste(as.character(tkgetOpenFile(title = "Read File",
                                                  filetypes = '{"Select File" {"*.*"}}',initialfile = "*.*")),
                       sep = "", collapse =" ")
AnaData <- import(SelectData)

#Exsample:Output data to SPSS file
###Creating Data
#####
#Install the tidyverse package if it is not already there
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
set.seed(1234)
n <- 300
TestData <- tibble(Group = sample(paste0("Group", 1:4), n,
                                  replace = TRUE),
                   X_num_Data = sample(c(c(1:30), c(100:175)),
                                       n, replace = TRUE),
                   Y_num_Data = sample(c(51:100), n, replace = TRUE))

setwd(paste(as.character(tkchooseDirectory(title = "Save Directory"), sep = "", collapse ="")))

#Save::export command
export(TestData, "TestData.sav")

I hope this makes your analysis a little easier !!

Amazon audibleの登録の紹介

プライム会員限定で2024年7月22日まで3か月無料体験キャンペーン開催中です。無料体験後は月額1,500円で聞き放題です。なお、聞き放題対象外の本はAudible会員であれば非会員価格の30%引きで購入することが可能です。

Amazon audibleはプロのナレーターが朗読した本をアプリで聞くことができるサービスで、オフライン再生も可能です。通勤や作業のお供にAmazon audibleのご登録はいかがでしょうか。

・AmazonのAudible

https://amzn.to/3L4FI5o

Copied title and URL