R table()输出到csv文件

时间:2019-12-16 00:41:00

标签: r write.table

我分别使用R函数,table()和write.table()来计数单元格数,并以.csv文件的形式保存到本地方向。 但是有两个问题:

  1. 生成的表的标题是月份的集合,但排名是根据首字母而不是日历月份的顺序排列。
    我知道一种解决方案是将月份分解为因子,但是数据包含40多个月。
    那么,有没有其他解决方案?

  2. 生成的表标题长度比表宽度小一,因此,在.csv文件中,标题不是其列的正确位置,而是下一列。

TabCDY <- table(DayNobyCounter$CounterID, DayNobyCounter$Year)
write.table(TabCDY, file = "Tab_CounterDayNumber_Year.csv", sep = ",", quote = FALSE, row.names = TRUE )

the output for a .csv file

1 个答案:

答案 0 :(得分:2)

在这种情况下,您可能会发现使用data.frame(或其堂兄,例如data.table)更容易。

因数分解的月数无关紧要,因为排序应自动完成。

样本数据:

library(data.table)
NN = 1e5
set.seed(39439)
DT = data.table(
  CounterID = sample(LETTERS, NN, TRUE),
  # integer values of 2019-01-01 -> 2019-12-31
  date = format(.Date(sample(17897:18261, NN, TRUE)), '%Y %b')
)

您可以尝试:

# old approach
table(DT$CounterID, DT$date)

# suggested approach
levels = unique(DT$date)
# %Y %b is not enough for as.Date, we have to manually supply the day too
levels_date = as.Date(paste(levels, '01'), format = '%Y %b %d')
# sort by the order as a Date (i.e. chronologically)
levels = levels[order(levels_date)]
# levels determines the sorting order
DT[ , date := factor(date, levels = levels)]
# dcast automatically uses this sorting order
TabCDY = DT[ , dcast(.SD, CounterID ~ date, fun.aggregate = length)]
#output to file
fwrite(TabCDY, 'Tab_CounterDayNumber_Year.csv')