這些函數相當於write_csv()等,但它們不是寫入磁盤,而是返回一個字符串。
用法
format_delim(
  x,
  delim,
  na = "NA",
  append = FALSE,
  col_names = !append,
  quote = c("needed", "all", "none"),
  escape = c("double", "backslash", "none"),
  eol = "\n",
  quote_escape = deprecated()
)
format_csv(
  x,
  na = "NA",
  append = FALSE,
  col_names = !append,
  quote = c("needed", "all", "none"),
  escape = c("double", "backslash", "none"),
  eol = "\n",
  quote_escape = deprecated()
)
format_csv2(
  x,
  na = "NA",
  append = FALSE,
  col_names = !append,
  quote = c("needed", "all", "none"),
  escape = c("double", "backslash", "none"),
  eol = "\n",
  quote_escape = deprecated()
)
format_tsv(
  x,
  na = "NA",
  append = FALSE,
  col_names = !append,
  quote = c("needed", "all", "none"),
  escape = c("double", "backslash", "none"),
  eol = "\n",
  quote_escape = deprecated()
)
參數
- x
 - 
一個 DataFrame 。
 - delim
 - 
用於分隔值的分隔符。對於
write_delim()默認為" ",對於write_excel_csv()默認為",",對於write_excel_csv2()默認為";"。必須是單個字符。 - na
 - 
用於缺失值的字符串。默認為 NA。缺失值永遠不會被引用;與
na具有相同值的字符串將始終被引用。 - append
 - 
如果
FALSE,將覆蓋現有文件。如果TRUE,將追加到現有文件。在這兩種情況下,如果文件不存在,則會創建新文件。 - col_names
 - 
如果
FALSE,列名將不會包含在文件頂部。如果TRUE,將包含列名稱。如果未指定,col_names將采用給定append的相反值。 - quote
 - 
如何處理包含需要引用的字符的字段。
- 
needed- 僅在需要時才引用值:如果它們包含分隔符、引號或換行符。 - 
all- 引用所有字段。 - 
none- 切勿引用字段。 
 - 
 - escape
 - 
當數據中存在引號時要使用的轉義類型。
- 
double- 引號通過加倍來轉義。 - 
backslash- 引號通過前麵的反斜杠轉義。 - 
none- 引號不會轉義。 
 - 
 - eol
 - 
要使用的行結束符。最常見的是
"\n"用於 Unix 樣式換行符,或"\r\n"用於 Windows 樣式換行符。 - quote_escape
 
輸出
因子是被迫的。使用 grisu3 算法將雙精度數格式化為十進製字符串。POSIXct值的格式為帶有 UTC 時區的 ISO8601注意:本地或非 UTC 時區的 POSIXct 對象在寫入前將轉換為 UTC 時間。
所有列均編碼為 UTF-8。 write_excel_csv() 和 write_excel_csv2() 還包括 UTF-8 Byte order mark,它向 Excel 指示 csv 是 UTF-8 編碼的。
創建 write_excel_csv2() 和 write_csv2 是為了允許具有不同區域設置的用戶使用默認設置保存 .csv 文件(例如,; 作為列分隔符,, 作為小數點分隔符)。這在一些歐洲國家很常見。
僅當值包含逗號、引號或換行符時才用引號引起來。
如果給出適當的擴展名,write_*() 函數將自動壓縮輸出。目前支持三種擴展:.gz(用於 gzip 壓縮)、.bz2(用於 bzip2 壓縮)和 .xz(用於 lzma 壓縮)。請參閱示例以獲取更多信息。
參考
Florian Loitsch,使用整數快速準確地打印浮點數,PLDI '10,http://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf
例子
# format_()* functions are useful for testing and reprexes
cat(format_csv(mtcars))
#> mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb
#> 21,6,160,110,3.9,2.62,16.46,0,1,4,4
#> 21,6,160,110,3.9,2.875,17.02,0,1,4,4
#> 22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
#> 21.4,6,258,110,3.08,3.215,19.44,1,0,3,1
#> 18.7,8,360,175,3.15,3.44,17.02,0,0,3,2
#> 18.1,6,225,105,2.76,3.46,20.22,1,0,3,1
#> 14.3,8,360,245,3.21,3.57,15.84,0,0,3,4
#> 24.4,4,146.7,62,3.69,3.19,20,1,0,4,2
#> 22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2
#> 19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4
#> 17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4
#> 16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3
#> 17.3,8,275.8,180,3.07,3.73,17.6,0,0,3,3
#> 15.2,8,275.8,180,3.07,3.78,18,0,0,3,3
#> 10.4,8,472,205,2.93,5.25,17.98,0,0,3,4
#> 10.4,8,460,215,3,5.424,17.82,0,0,3,4
#> 14.7,8,440,230,3.23,5.345,17.42,0,0,3,4
#> 32.4,4,78.7,66,4.08,2.2,19.47,1,1,4,1
#> 30.4,4,75.7,52,4.93,1.615,18.52,1,1,4,2
#> 33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1
#> 21.5,4,120.1,97,3.7,2.465,20.01,1,0,3,1
#> 15.5,8,318,150,2.76,3.52,16.87,0,0,3,2
#> 15.2,8,304,150,3.15,3.435,17.3,0,0,3,2
#> 13.3,8,350,245,3.73,3.84,15.41,0,0,3,4
#> 19.2,8,400,175,3.08,3.845,17.05,0,0,3,2
#> 27.3,4,79,66,4.08,1.935,18.9,1,1,4,1
#> 26,4,120.3,91,4.43,2.14,16.7,0,1,5,2
#> 30.4,4,95.1,113,3.77,1.513,16.9,1,1,5,2
#> 15.8,8,351,264,4.22,3.17,14.5,0,1,5,4
#> 19.7,6,145,175,3.62,2.77,15.5,0,1,5,6
#> 15,8,301,335,3.54,3.57,14.6,0,1,5,8
#> 21.4,4,121,109,4.11,2.78,18.6,1,1,4,2
cat(format_tsv(mtcars))
#> mpg	cyl	disp	hp	drat	wt	qsec	vs	am	gear	carb
#> 21	6	160	110	3.9	2.62	16.46	0	1	4	4
#> 21	6	160	110	3.9	2.875	17.02	0	1	4	4
#> 22.8	4	108	93	3.85	2.32	18.61	1	1	4	1
#> 21.4	6	258	110	3.08	3.215	19.44	1	0	3	1
#> 18.7	8	360	175	3.15	3.44	17.02	0	0	3	2
#> 18.1	6	225	105	2.76	3.46	20.22	1	0	3	1
#> 14.3	8	360	245	3.21	3.57	15.84	0	0	3	4
#> 24.4	4	146.7	62	3.69	3.19	20	1	0	4	2
#> 22.8	4	140.8	95	3.92	3.15	22.9	1	0	4	2
#> 19.2	6	167.6	123	3.92	3.44	18.3	1	0	4	4
#> 17.8	6	167.6	123	3.92	3.44	18.9	1	0	4	4
#> 16.4	8	275.8	180	3.07	4.07	17.4	0	0	3	3
#> 17.3	8	275.8	180	3.07	3.73	17.6	0	0	3	3
#> 15.2	8	275.8	180	3.07	3.78	18	0	0	3	3
#> 10.4	8	472	205	2.93	5.25	17.98	0	0	3	4
#> 10.4	8	460	215	3	5.424	17.82	0	0	3	4
#> 14.7	8	440	230	3.23	5.345	17.42	0	0	3	4
#> 32.4	4	78.7	66	4.08	2.2	19.47	1	1	4	1
#> 30.4	4	75.7	52	4.93	1.615	18.52	1	1	4	2
#> 33.9	4	71.1	65	4.22	1.835	19.9	1	1	4	1
#> 21.5	4	120.1	97	3.7	2.465	20.01	1	0	3	1
#> 15.5	8	318	150	2.76	3.52	16.87	0	0	3	2
#> 15.2	8	304	150	3.15	3.435	17.3	0	0	3	2
#> 13.3	8	350	245	3.73	3.84	15.41	0	0	3	4
#> 19.2	8	400	175	3.08	3.845	17.05	0	0	3	2
#> 27.3	4	79	66	4.08	1.935	18.9	1	1	4	1
#> 26	4	120.3	91	4.43	2.14	16.7	0	1	5	2
#> 30.4	4	95.1	113	3.77	1.513	16.9	1	1	5	2
#> 15.8	8	351	264	4.22	3.17	14.5	0	1	5	4
#> 19.7	6	145	175	3.62	2.77	15.5	0	1	5	6
#> 15	8	301	335	3.54	3.57	14.6	0	1	5	8
#> 21.4	4	121	109	4.11	2.78	18.6	1	1	4	2
cat(format_delim(mtcars, ";"))
#> mpg;cyl;disp;hp;drat;wt;qsec;vs;am;gear;carb
#> 21;6;160;110;3.9;2.62;16.46;0;1;4;4
#> 21;6;160;110;3.9;2.875;17.02;0;1;4;4
#> 22.8;4;108;93;3.85;2.32;18.61;1;1;4;1
#> 21.4;6;258;110;3.08;3.215;19.44;1;0;3;1
#> 18.7;8;360;175;3.15;3.44;17.02;0;0;3;2
#> 18.1;6;225;105;2.76;3.46;20.22;1;0;3;1
#> 14.3;8;360;245;3.21;3.57;15.84;0;0;3;4
#> 24.4;4;146.7;62;3.69;3.19;20;1;0;4;2
#> 22.8;4;140.8;95;3.92;3.15;22.9;1;0;4;2
#> 19.2;6;167.6;123;3.92;3.44;18.3;1;0;4;4
#> 17.8;6;167.6;123;3.92;3.44;18.9;1;0;4;4
#> 16.4;8;275.8;180;3.07;4.07;17.4;0;0;3;3
#> 17.3;8;275.8;180;3.07;3.73;17.6;0;0;3;3
#> 15.2;8;275.8;180;3.07;3.78;18;0;0;3;3
#> 10.4;8;472;205;2.93;5.25;17.98;0;0;3;4
#> 10.4;8;460;215;3;5.424;17.82;0;0;3;4
#> 14.7;8;440;230;3.23;5.345;17.42;0;0;3;4
#> 32.4;4;78.7;66;4.08;2.2;19.47;1;1;4;1
#> 30.4;4;75.7;52;4.93;1.615;18.52;1;1;4;2
#> 33.9;4;71.1;65;4.22;1.835;19.9;1;1;4;1
#> 21.5;4;120.1;97;3.7;2.465;20.01;1;0;3;1
#> 15.5;8;318;150;2.76;3.52;16.87;0;0;3;2
#> 15.2;8;304;150;3.15;3.435;17.3;0;0;3;2
#> 13.3;8;350;245;3.73;3.84;15.41;0;0;3;4
#> 19.2;8;400;175;3.08;3.845;17.05;0;0;3;2
#> 27.3;4;79;66;4.08;1.935;18.9;1;1;4;1
#> 26;4;120.3;91;4.43;2.14;16.7;0;1;5;2
#> 30.4;4;95.1;113;3.77;1.513;16.9;1;1;5;2
#> 15.8;8;351;264;4.22;3.17;14.5;0;1;5;4
#> 19.7;6;145;175;3.62;2.77;15.5;0;1;5;6
#> 15;8;301;335;3.54;3.57;14.6;0;1;5;8
#> 21.4;4;121;109;4.11;2.78;18.6;1;1;4;2
# Specifying missing values
df <- data.frame(x = c(1, NA, 3))
format_csv(df, na = "missing")
#> [1] "x\n1\nmissing\n3\n"
# Quotes are automatically added as needed
df <- data.frame(x = c("a ", '"', ",", "\n"))
cat(format_csv(df))
#> x
#> a 
#> """"
#> ","
#> "
#> "
相關用法
- R readr datasource 創建源對象。
 - R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
 - R readr read_rds 讀/寫 RDS 文件。
 - R readr read_lines 從文件中讀取/寫入行
 - R readr parse_number 靈活地解析數字
 - R readr read_fwf 將固定寬度文件讀入 tibble
 - R readr read_builtin 從包中讀取內置對象
 - R readr Tokenizers 分詞器。
 - R readr melt_table 返回空格分隔文件中每個標記的熔化數據
 - R readr date_names 創建或檢索日期名稱
 - R readr type_convert 重新轉換現有 DataFrame 中的字符列
 - R readr locale 創建語言環境
 - R readr write_delim 將數據幀寫入分隔文件
 - R readr parse_vector 解析字符向量。
 - R readr with_edition 暫時更改活動閱讀器版本
 - R readr read_delim 將分隔文件(包括 CSV 和 TSV)讀入 tibble
 - R readr edition_get 檢索當前活動版本
 - R readr readr_example 獲取 readr 示例的路徑
 - R readr melt_fwf 返回固定寬度文件中每個標記的熔化數據
 - R readr count_fields 計算文件每一行中的字段數
 - R readr read_table 將空格分隔的列讀入 tibble
 - R readr problems 檢索解析問題
 - R readr parse_guess 使用“最佳”類型進行解析
 - R readr parse_datetime 解析日期/時間
 - R readr read_file 讀/寫完整文件
 
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Convert a data frame to a delimited string。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
