read.fwf

桁数を指定して読み込む。

「031fixedlen.txt」の内容
10011234411063129000650
10021233409093168000350
# 固定長形式などのファイルを桁数指定で読み込む
x <- read.fwf("031fixedlen.txt", widths=c(4,5,2,3,5,4), colClasses = "character")
x
##     V1    V2 V3  V4    V5   V6
## 1 1001 12344 11 063 12900 0650
## 2 1002 12334 09 093 16800 0350

readLines, writeLines

一度に一行分を取り込む。 n = 行指定(-1Lで最後まで読み込む)

「031binary.txt」の内容
101001110101010100101110100100111010010001001010100101010010000
101001110101010100101110100100111010010001001010100101010010000
101001110101010100101110100100111010010001001010100101010010000
101001110101010100101110100100111010010001001010100101010010000
# binary130.txtを読み込む
x <- readLines("031binary.txt", n = -1L)
x
## [1] "101001110101010100101110100100111010010001001010100101010010000"
## [2] "101001110101010100101110100100111010010001001010100101010010000"
## [3] "101001110101010100101110100100111010010001001010100101010010000"
## [4] "101001110101010100101110100100111010010001001010100101010010000"

writeLines

1行単位にファイルに書き込む

tmp <- file(tempfile(tmpdir = "c:\\Users\\data\\R\\"))
open(tmp, "w")
writeLines("1行目:いろはにほへと", tmp)
writeLines("2行目:ちりぬるおわか", tmp)
close(tmp)
tempファイルの内容:
1行目:いろはにほへと
2行目:ちりぬるおわか

readRDS, writeRDS

Rのオブジェクトをファイルの保存する、その保存したものを読み込む。

x <- cor.test(x = mtcars$mpg, y = mtcars$wt, method="pearson")
x
## 
##  Pearson's product-moment correlation
## 
## data:  mtcars$mpg and mtcars$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9338264 -0.7440872
## sample estimates:
##        cor 
## -0.8676594
saveRDS(x, "031mtcars_mpg_wt.Rds")
y <- readRDS("031mtcars_mpg_wt.Rds")
y
## 
##  Pearson's product-moment correlation
## 
## data:  mtcars$mpg and mtcars$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9338264 -0.7440872
## sample estimates:
##        cor 
## -0.8676594

load, save

特定のオブジェクトをRの内部形式で保存する。その保存されたものを読み込む。

x <- cor.test(x = mtcars$mpg, y = mtcars$wt, method="pearson")
x
## 
##  Pearson's product-moment correlation
## 
## data:  mtcars$mpg and mtcars$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9338264 -0.7440872
## sample estimates:
##        cor 
## -0.8676594
save(x, file = "031mtcars_mpg_wt.Rdata")
# オブジェクトの削除
rm(x)
load("031mtcars_mpg_wt.Rdata")
x
## 
##  Pearson's product-moment correlation
## 
## data:  mtcars$mpg and mtcars$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9338264 -0.7440872
## sample estimates:
##        cor 
## -0.8676594

library(foreign)

S,SAS,SPSS,Stataなど外部アプリケーションのデータを読み書きする。

test.df <- data.frame(id=c("1","2","3","4","5"), value=c(12,33,56,23,77,89))
# SAS向けファイルの作成
write.foreign(df=test.df, datafile="031test.csv", codefile="031test.sas", package="SAS")

引用:Stackoveflow:Can anyone help me write a R data frame as a SAS data set?

最終更新日:2016/04/27

copyrigth © 2016 r-beginners.com All rigths reserved.

PAGE TOP ▲