在r中创建表

在r中创建表,r,R,我已经开发了以下代码来在r中创建一个表,并将其导出到csv,在csv中我将其格式化以插入到word文档中。此示例包含每个水果的总数以及销售月份 library (lubridate) Table<-read.csv("Fruit.csv") #Creates a month name column Table$Month<- month(Table$Date, label=TRUE) #Creates the frequency table FreqTable<-addma

我已经开发了以下代码来在r中创建一个表,并将其导出到csv,在csv中我将其格式化以插入到word文档中。此示例包含每个水果的总数以及销售月份

library (lubridate)
Table<-read.csv("Fruit.csv")

#Creates a month name column
Table$Month<- month(Table$Date, label=TRUE)

#Creates the frequency table
FreqTable<-addmargins(table(Table$Fruit, Table$Month))

#Exports to CSV
write.csv(FreqTable,"Table.csv")
库(lubridate)

表某些数据已采用频率格式

fruit <- data.frame(Jan = c(4, 5, 2), Feb = c(1, 3, 0), March = c(0, 0, 0), row.names = c("Apples", "Grapes", "Oranges"))

fruit$sum <- rowSums(fruit)
fruit <- fruit[!colSums(fruit > 0) == 0] # Subset your columns with sums > 0
colnames(fruit)[3] <- "Total" # Rename column

fruit无法从示例中判断第一列是列还是行名称列表
str(Table)
将告诉您水果列是列还是行名向量。这将给你两种不同的解决方案。要更改列名,请使用
names(object)[column]欢迎使用stackoverflow。看看你的问题。