java导出csv文件_R语言数据导入与导出
R語言數(shù)據(jù)導(dǎo)入與導(dǎo)出
整這么一個(gè)系列,還是因?yàn)閷W(xué)R語言時(shí)遇到過一個(gè)非?!靶“子押谩钡木W(wǎng)站“DataScience Made Simple”。相信很多人搜到過這個(gè)網(wǎng)站,或許你在意到或許并沒在意。年前試著和作者發(fā)了一封郵件,想要把他這個(gè)網(wǎng)站做成漢語版的帖子發(fā)在公眾號上,讓我感動(dòng)的是作者團(tuán)隊(duì)欣然同意。于是就想著搞這么一個(gè)系列,能不能堅(jiān)持下來還不好說……且行且珍惜吧。
在用R語言分析數(shù)據(jù)時(shí),我們首先要進(jìn)行數(shù)據(jù)的導(dǎo)入與導(dǎo)出。R支持多種文件格式,包括但不限于常見的txt,csv,xlsx,和tsv等等。數(shù)據(jù)的導(dǎo)入依賴于各種R包,方法大同小異,可根據(jù)實(shí)際情況舉一反三。本次將簡要介紹以下實(shí)用技巧:
- 怎樣在R中導(dǎo)入 .csv 文件
- 怎樣在R中導(dǎo)出 .csv 文件
- 怎樣在R中導(dǎo)入 .xlsx 文件
- 怎樣在R中導(dǎo)出 .xlsx 文件
在R中獲取和設(shè)置工作路徑
在讀取數(shù)據(jù)之前我們需要設(shè)定文件位置,以便找到相應(yīng)文件并進(jìn)行讀取。
用 setwd()函數(shù)可以把特定位置設(shè)置成為工作路徑。getwd()函數(shù)可以獲取當(dāng)前工作路徑。
# Get the current working directory.getwd()
# output will be "C:/Users/username/Documents"
# Set current working directory.
setwd("D:/Folder_name")
# now the working directory has been set to Folder_name in D Drive
# Get the current working directory.
getwd()
# output will be "D:/Folder_name"
在R中導(dǎo)出 .CSV 文件
read.csv()是R中默認(rèn)的讀取.csv文件的函數(shù),是read.table()函數(shù)的簡易形式,讀.csv文件時(shí)非常方便實(shí)用。
- 如果所需文件在你的當(dāng)前工作路徑中:
mydata = read.csv("input.csv")
# reads the csv file in R object named mydata
print(mydata)
#print()可省略
mydata
- 如果所需文件不在當(dāng)前工作路徑中,則需要在文件前指定所在位置:以下兩種形式“/”或“\\”皆可用于指定路徑。
mydata = read.csv("D:/other_folder/input.csv")
mydata = read.csv("D:\\other_folder\\input.csv")
#reads the file named "input.csv" from "other_folder" in "D drive"
print(mydata)
上述示例中將所需文件導(dǎo)入R中并儲存為“mydata”。
在R中將數(shù)據(jù)導(dǎo)出為.csv文件
在R中導(dǎo)出數(shù)據(jù)為.csv文件時(shí)使用 write.csv()函數(shù)。以下示例分別為將mydata導(dǎo)出到當(dāng)前工作路徑或到指定路徑。
# Write data into a csv file in Rwrite.csv(mydata,"output.csv")
# contents in the object “mydata” are written to a csv file named “output.csv” in Current working directory
Write.csv(mydata, “D:/other_folder/output.csv”)
# similarly to write outside the working directory you have to provide the path along with file name`
在R中導(dǎo)入Excel數(shù)據(jù)
Microsoft Excel是使用最廣泛的電子表格程序,它以.xls或.xlsx格式存儲數(shù)據(jù)。R可以使用一些特定的包直接從這些文件中讀取數(shù)據(jù)。較常用的包有openxlsx、xlsx、gdata等。我們將使用openxlsx包進(jìn)行演示。這個(gè)包安裝起來較方便,而不像xlsx包那樣在安裝時(shí)容易出錯(cuò)。但是openxlsx包只支持讀取.xlsx文件,而不支持.xls格式。 R也可以用這個(gè)包寫入excel文件。
# read data from excel (.xlsx) file in Rinstall.packages("openxlsx")
library(openxlsx)
# to read the data from nth sheet (say 4)
mydata = read.xlsx("D:/myexcel.xlsx", sheet = 4)
mydata
在R中將數(shù)據(jù)導(dǎo)出excel文件 (.xlsx):
openxlsx包中的write.xlsx()函數(shù)可用于將數(shù)據(jù)導(dǎo)入excel文件。
# write data into excel (.xlsx) file in R#install.packages("openxlsx")
library(openxlsx)
# data in the object “mydata” is written in a file named ”dummy.xlsx” in D drive with sheet named ”Newdata”
write.xlsx(mydata,"D:/dummy.xlsx")
write.xlsx是導(dǎo)出數(shù)據(jù)的簡易方式。關(guān)于導(dǎo)出的更多參數(shù)設(shè)置,可在R中?openxlsx::writeData()查詢。所有可設(shè)置參數(shù)如下:
writeData(wb,
sheet,
x,
startCol = 1,
startRow = 1,
xy = NULL,
colNames = TRUE,
rowNames = FALSE,
headerStyle = NULL,
borders = c("none", "surrounding", "rows", "columns", "all"),
borderColour = getOption("openxlsx.borderColour", "black"),
borderStyle = getOption("openxlsx.borderStyle", "thin"),
withFilter = FALSE,
keepNA = FALSE,
na.string = NULL,
name = NULL,
sep = ", "
)
撰寫過程有所更新或調(diào)整。點(diǎn)擊“閱讀原文”直達(dá)英文網(wǎng)站原文(有廣告彈窗)。
== 更多干貨 關(guān)注直達(dá) ==
火山圖 | share legend | 柱狀圖 | 箱線圖 | 提琴圖 | 杰特圖 | 分組柱狀圖 | 分組小提琴圖 | 任意雙拼 | 金字塔圖 | circlize和弦圖 | 山巒圖 | 相關(guān)性和弦圖 | 分面小提琴圖 | 火山圖美化 | 配色 | R爬蟲 | 3Dbarplot | 臨床數(shù)據(jù)組合 | 和弦圖2 | 對角線熱圖
總結(jié)
以上是生活随笔為你收集整理的java导出csv文件_R语言数据导入与导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: activemq 开启监听_Spring
- 下一篇: r语言中的或怎么表示什么不同_R经典入门