简体   繁体   English

如何将 R data.frame 保存为没有空格的 CSV?

[英]How to save an R data.frame as CSV with no spaces?

I have the following data.frame that I am saving as CSV file.我有以下data.frame保存为CSV文件。 When the data.frame is saved as CSV I see one point space after the Text component (see the image).data.frame保存为CSV我会在Text组件后面看到一个点空间(见图)。 How to avoid the space creation while saving data.frame as CSV ?如何在将data.frame保存为CSV时避免空间创建? Thank you谢谢

library(lubridate)
library(tidyverse)

DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2001-12-31"), by = "days"), Flow = runif(365, 1, 5)) %>% 
      mutate(Year = year(Date), Month = month(Date), Day = day(Date), JDay = yday(Date), Text = rep("FLOW_OUT_",365)) %>% 
      mutate(Format = paste(Text,JDay,"_",Year)) %>% 
      select(8) %>% 
      write.csv(file = "Format.csv")

在此处输入图片说明

We can use paste0 instead of paste to avoid creating the spaces as the default sep option in paste is sep= " "我们可以使用paste0而不是paste来避免创建空格,因为paste的默认sep选项是sep= " "

...
  mutate(Format = paste0(Text,JDay,"_",Year))
...

-full code -完整代码

DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2001-12-31"), 
         by = "days"), Flow = runif(365, 1, 5)) %>% 
      mutate(Year = year(Date), Month = month(Date),
      Day = day(Date), JDay = yday(Date), Text = rep("FLOW_OUT_",365)) %>% 
      mutate(Format = paste0(Text,JDay,"_",Year)) %>% 
      select(8)

Or another option is making use of sep argument in paste或者另一种选择是在paste使用sep参数

DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2001-12-31"), 
             by = "days"), Flow = runif(365, 1, 5)) %>% 
          mutate(Year = year(Date), Month = month(Date),
          Day = day(Date), JDay = yday(Date), Text = rep("FLOW_OUT_",365)) %>% 
          mutate(Format = paste(Text,JDay,"_",Year, sep="")) %>% 
          select(8)

-output -输出

head(DF)
#           Format
#1 FLOW_OUT_1_2001
#2 FLOW_OUT_2_2001
#3 FLOW_OUT_3_2001
#4 FLOW_OUT_4_2001
#5 FLOW_OUT_5_2001
#6 FLOW_OUT_6_2001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM