如何使用带记事本可识别的换行符的R保存CSV文件?

时间:2016-08-04 13:57:57

标签: r

很抱歉打扰你,可能是一个编码问题。花了几个小时没有得到解决方案我决定在这里发布。 我试图使用来自Ubuntu 14.04的write.tablewrite.csvwrite.csv2来写一个简单的表失败。我的数据因cronjob而变得混乱:

ID <- c("",30,26,20,30,40,5,10,4)
b <- c("",2233,12,2,22,13,23,23,100)
c <- c("","","","","","","","","")
d <- c("","","","","","","","","")
e <- c("","","","","","800","","","")
f <- c("","","","","","","","","")
g <- c("","","","","","","","EA","")
h <- c("","","","","","","","","")
df <- data.frame(ID,b,c,d,e,f,g,h)

  # change columns to chr
for(i in c(1,2:ncol(df))) {
 df[,i] <- as.character(df[,i])
}

str(df)

# data.frame':  9 obs. of  8 variables:
#  $ ID: chr  "" "30" "26" "20" ...
#  $ b : chr  "" "2233" "12" "2" ...
#  $ c : chr  "" "" "" "" ...
#  $ d : chr  "" "" "" "" ...
#  $ e : chr  "" "" "" "" ...
#  $ f : chr  "" "" "" "" ...
# $ g :  chr  "" "" "" "" ...
#  $ h : chr  "" "" "" "" ...


head(df,n=9)

    ID    b c d   e   f  g  h
# 1                       
# 2 30 2233               
# 3 26   12               
# 4 20    2               
# 5 30   22               
# 6 40   13      800       
# 7  5   23               
# 8 10   23              EA  
# 9  4  100               

我尝试过在SO上找到的不同组合和建议,但没有任何效果。结果总是以某种方式取代而不是长期的。在当前示例中,只有一行。

我试过了:

write.table(df,"df.csv",row.names = FALSE, dec=".",sep=";")
write.table(df,"df.csv",row.names = FALSE,dec=".",sep=";", col.names = T)
write.table(df,"df.csv",row.names = FALSE,sep=";",fileEncoding = "UTF-8")
write.table(df,"df.csv",row.names = FALSE,fileEncoding = "UTF-8")

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
[1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C                   LC_TIME=de_DE.UTF-8       
 [4] LC_COLLATE=de_DE.UTF-8     LC_MONETARY=de_DE.UTF-8        LC_MESSAGES=de_DE.UTF-8   
 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8   LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3   DBI_0.4-1     RGA_0.4.2     RMySQL_0.11-3

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5     lubridate_1.5.6 digest_0.6.9    assertthat_0.1    R6_2.1.2       
 [6] plyr_1.8.3      jsonlite_1.0    magrittr_1.5    httr_1.1.0         stringi_1.1.1  
[11] curl_0.9.7      tools_3.3.1     stringr_1.0.0   parallel_3.3.1 

错误的输出为pic:

enter image description here

在以下相同数据中更正输出结果: enter image description here

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
[![enter image description here][2]][2]

1 个答案:

答案 0 :(得分:4)

问题不在于R或Ubuntu它是记事本。具体来说,它预计会有"\r\n"换行符,而大多数其他文字阅读器会对"\n"感到满意,write.xxxeol="\r\n"使用的默认换行符。

如果您添加参数write.table(df,"df.csv",row.names = FALSE, dec=".",sep=";",eol="\r\n") ,那么您应该可以在记事本中打开并查看预期的换行符。

例如:

MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(context.getExternalFilesDir("krishna").getPath() + "/amalharinaam_finalcut_loud.mp3"));
mPlayer.prepareAsync();