爲什麼不把csv寫成(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) 4)(在記事本中查看時)Write.table不能用逗號分隔
results <- data.frame(Answer= rep(4,16))
write.table(results$Answer, file = "paul.csv", row.names=FALSE, col.names=FALSE, sep=",")
這是因爲如果它忽略了九月的說法..? Paul
爲什麼不把csv寫成(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) 4)(在記事本中查看時)Write.table不能用逗號分隔
results <- data.frame(Answer= rep(4,16))
write.table(results$Answer, file = "paul.csv", row.names=FALSE, col.names=FALSE, sep=",")
這是因爲如果它忽略了九月的說法..? Paul
您已經指定了一個垂直向量。當被寫入的垂直矢量,每個矢量元素被寫入到自己的行:
> results <- data.frame(Answer= rep(4,16))
> dim(results)
[1] 16 1
> results
Answer
1 4
2 4
3 4
4 4
5 4
6 4
7 4
8 4
9 4
10 4
11 4
12 4
13 4
14 4
15 4
16 4
如果你想要寫在一行的矢量,則可能調換一個垂直矢量成使用功能t()
一個水平矢量:
> results <- t(data.frame(Answer= rep(4,16)))
> dim(results)
[1] 1 16
> results
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
Answer 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
的數據然後寫入文件:
> results <- t(data.frame(Answer= rep(4,16)))
> write.table(results$Answer, file = "paul.csv", row.names=FALSE, col.names=FALSE, sep=",")
將產生以下內容:
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
對不起,但是如果你轉置一個df,結果是class'matrix',結果$ Answer'會拋出一個錯誤。它必須是'results [「Answer」,]'。 –
,如果你想sep
,因爲sep = ...
作品使用列,應該定義變量作爲一列。這是你可以做到的一種方式:
results <- data.frame(t(rep(4,16)))
write.table(results, file = "paul.csv", row.names=FALSE, col.names=FALSE, sep=",")
有兩種簡單的可能性。
一,使用參數eol = ","
就像我在上面的評論中所說的那樣。這種方法的問題在於它將在行尾包含一個額外的","
,因爲write.table
總是包含一個結束換行符。
write.table(results$Answer, file = "paul.csv", row.names = FALSE, col.names = FALSE, eol = ",")
另一種方法是使用cat
。
cat(results$Answer, file = "paul.csv", sep = ",")
我相信這是你想要的。
我認爲csv的默認分隔符是「;」 –
@ Guillaume.P不,那不是。問題是分隔符分隔**列**而不是行。 –
我如何分隔行..? – PaulBeales