如何将 R data.frame 保存为没有空格的 CSV?
Posted
技术标签:
【中文标题】如何将 R data.frame 保存为没有空格的 CSV?【英文标题】:How to save an R data.frame as CSV with no spaces? 【发布时间】:2020-10-21 19:44:38 【问题描述】:我有以下data.frame
保存为CSV
文件。当data.frame
保存为CSV
时,我在Text
组件之后看到一个点空间(见图)。如何在将data.frame
保存为CSV
时避免空间创建?谢谢
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")
【问题讨论】:
【参考方案1】:我们可以使用paste0
代替paste
来避免创建空格,因为paste
中的默认sep
选项是sep= " "
...
mutate(Format = paste0(Text,JDay,"_",Year))
...
-完整代码
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)
或者另一种选择是在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)
-输出
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
【讨论】:
以上是关于如何将 R data.frame 保存为没有空格的 CSV?的主要内容,如果未能解决你的问题,请参考以下文章
如何打开多个 .RDATA 并将其中一个名称保存为 data.frame