java导出csv文件_R语言数据导入与导出
R語言數(shù)據(jù)導入與導出
整這么一個系列,還是因為學R語言時遇到過一個非?!靶“子押谩钡木W(wǎng)站“DataScience Made Simple”。相信很多人搜到過這個網(wǎng)站,或許你在意到或許并沒在意。年前試著和作者發(fā)了一封郵件,想要把他這個網(wǎng)站做成漢語版的帖子發(fā)在公眾號上,讓我感動的是作者團隊欣然同意。于是就想著搞這么一個系列,能不能堅持下來還不好說……且行且珍惜吧。
在用R語言分析數(shù)據(jù)時,我們首先要進行數(shù)據(jù)的導入與導出。R支持多種文件格式,包括但不限于常見的txt,csv,xlsx,和tsv等等。數(shù)據(jù)的導入依賴于各種R包,方法大同小異,可根據(jù)實際情況舉一反三。本次將簡要介紹以下實用技巧:
- 怎樣在R中導入 .csv 文件
- 怎樣在R中導出 .csv 文件
- 怎樣在R中導入 .xlsx 文件
- 怎樣在R中導出 .xlsx 文件
在R中獲取和設置工作路徑
在讀取數(shù)據(jù)之前我們需要設定文件位置,以便找到相應文件并進行讀取。
用 setwd()函數(shù)可以把特定位置設置成為工作路徑。getwd()函數(shù)可以獲取當前工作路徑。
# 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中導出 .CSV 文件
read.csv()是R中默認的讀取.csv文件的函數(shù),是read.table()函數(shù)的簡易形式,讀.csv文件時非常方便實用。
- 如果所需文件在你的當前工作路徑中:
mydata = read.csv("input.csv")
# reads the csv file in R object named mydata
print(mydata)
#print()可省略
mydata
- 如果所需文件不在當前工作路徑中,則需要在文件前指定所在位置:以下兩種形式“/”或“\\”皆可用于指定路徑。
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)
上述示例中將所需文件導入R中并儲存為“mydata”。
在R中將數(shù)據(jù)導出為.csv文件
在R中導出數(shù)據(jù)為.csv文件時使用 write.csv()函數(shù)。以下示例分別為將mydata導出到當前工作路徑或到指定路徑。
# 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中導入Excel數(shù)據(jù)
Microsoft Excel是使用最廣泛的電子表格程序,它以.xls或.xlsx格式存儲數(shù)據(jù)。R可以使用一些特定的包直接從這些文件中讀取數(shù)據(jù)。較常用的包有openxlsx、xlsx、gdata等。我們將使用openxlsx包進行演示。這個包安裝起來較方便,而不像xlsx包那樣在安裝時容易出錯。但是openxlsx包只支持讀取.xlsx文件,而不支持.xls格式。 R也可以用這個包寫入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ù)導出excel文件 (.xlsx):
openxlsx包中的write.xlsx()函數(shù)可用于將數(shù)據(jù)導入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是導出數(shù)據(jù)的簡易方式。關于導出的更多參數(shù)設置,可在R中?openxlsx::writeData()查詢。所有可設置參數(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)整。點擊“閱讀原文”直達英文網(wǎng)站原文(有廣告彈窗)。
== 更多干貨 關注直達 ==
火山圖 | share legend | 柱狀圖 | 箱線圖 | 提琴圖 | 杰特圖 | 分組柱狀圖 | 分組小提琴圖 | 任意雙拼 | 金字塔圖 | circlize和弦圖 | 山巒圖 | 相關性和弦圖 | 分面小提琴圖 | 火山圖美化 | 配色 | R爬蟲 | 3Dbarplot | 臨床數(shù)據(jù)組合 | 和弦圖2 | 對角線熱圖
總結
以上是生活随笔為你收集整理的java导出csv文件_R语言数据导入与导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: activemq 开启监听_Spring
- 下一篇: r语言中的或怎么表示什么不同_R经典入门