#Some Data
Sales <- data.frame(Appliance =c( "Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio"),Country=c("USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK"), Year=c(2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016))
#Producing a table
SalesTable<-(table(Sales[, c("Appliance", "Year")]))
#Exporting the table
write.csv(SalesTable,"Table.csv")
以上内容生成并导出一个表格,该表格汇总了按年出售的电器数量。原始表中还有一个“国家/地区”列,显示了在哪个国家/地区销售产品。我想要做的是插入一个循环,该循环生成表并专门针对每个国家/地区进行导出,并根据国家/地区名称标记导出内容。
答案 0 :(得分:0)
您实际上不需要循环。您可以使用tidyverse
方法,在其中可以基于map
在数据子集上使用Country
函数:
Sales <- data.frame(Appliance =c( "Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio"),
Country=c("USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK"),
Year=c(2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016))
library(tidyverse)
Results = Sales %>%
group_by(Country = as.character(Country)) %>% # for each country
nest() %>% # nest data
mutate(Table = map(data, ~table(.[, c("Appliance", "Year")]))) # create a table using your function
# see Results table
Results
# # A tibble: 3 x 3
# Country data Table
# <chr> <list> <list>
# 1 USA <tibble [12 x 2]> <S3: table>
# 2 Canada <tibble [12 x 2]> <S3: table>
# 3 UK <tibble [12 x 2]> <S3: table>
到目前为止,您已经创建了一个表(Results
),它告诉您,对于每个Country
,您都使用了数据集的一个子集(存储在列data
中)来创建一个表(存储在列Table
中)。
您可以使用Results$Table
(作为列表)或Results$Table[[1]]
等查看并检查这些表,以查看列表中的每个元素。
# save tables as .csv files and use the countries as file names
map2(Results$Country, Results$Table, ~write.csv(.y, paste0(.x, ".csv")))
此命令将使用表和相应的国家/地区在您的工作目录中创建.csv文件。
答案 1 :(得分:0)
基于Loop
的解决方案:(无需将数据存储到table
,因为您可以直接写入.csv
文件)
Sales <- data.frame(Appliance =c( "Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio"),Country=c("USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK"), Year=c(2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016))
# Extract unique country from the data i.e. USA, Canada, UK
country= unique(Sales$Country)
for(i in 1:length(country)){ # Loop over the each country
# Extract country wise data and store Appliance and Year to each file
write.csv(Sales[Sales$Country==country[i],c(1,3)],paste(country[i],"csv",sep = "."))
}
更新的解决方案
library(plyr)
Sales <- data.frame(Appliance =c( "Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio"),Country=c("USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK"), Year=c(2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016))
# Extract unique country from the data i.e. USA, Canada, UK
country= unique(Sales$Country)
for(i in 1:length(country)){ # Loop over the each country
# Extract country wise Appliance and Year data
a=Sales[Sales$Country==country[i],c(1,3)]
# Get the count for each appliances
counts <- ddply(a, .(a$Appliance,a$Year), nrow)
# Name the column
names(counts) <- c("Appliance", "Year", "Freq")
# Bind this count with country
data=cbind(country[i],counts)
# Now you can write this df to .csv file (country wise .csv file)
write.csv(data,paste(country[i],"csv",sep = "."))
}
答案 2 :(得分:0)
也许您需要一种更简单的方法来将数据获取到一个文件中:
library(dplyr)
by_coun <- Sales %>% group_by(Country,Appliance,Year)
by_couns <- by_coun %>% summarise(n = n())
ndf=data.frame(by_couns)
ndf
# > ndf
# Country Appliance Year n
# 1 Canada Laptop 2016 2
# 2 Canada Laptop 2017 2
# 3 Canada TV 2016 4
# 4 Canada TV 2017 4
# 5 UK Fridge 2016 4
# 6 UK Radio 2016 4
# 7 UK TV 2016 4
# 8 USA Laptop 2017 8
# 9 USA TV 2017 4
library(reshape)
ndfl <- melt(ndf, id=c("Appliance","Country","Year"), preserve=FALSE)
(res=reshape1(ndfl, list(c("Country","Appliance"),"Year"), sum))
# > (res=reshape1(ndfl, list(c("Country","Appliance"),"Year"), sum))
# Country Appliance 2016 2017
# 1 Canada Laptop 2 2
# 2 Canada TV 4 4
# 3 UK Fridge 4 NA
# 4 UK Radio 4 NA
# 5 UK TV 4 NA
# 6 USA Laptop NA 8
# 7 USA TV NA 4
write.csv(res,"Table.csv")
答案 3 :(得分:0)
由于这里发布的答案,以下代码回答了我的问题:
#Some data
Sales <- data.frame(Appliance =c( "Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio","Laptop", "TV", "Fridge","TV", "TV", "TV","Laptop", "Laptop", "Radio"),Country=c("USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK","USA","Canada","UK"), Year=c(2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016,2017,2016,2016,2017,2017,2016))
# Extract unique country from the data i.e. USA, Canada, UK
country<- unique(Sales$Country)
for(i in 1:length(country)){ # Loop over the each country
# Extract Appliance and Year data for each country
a<-Sales[Sales$Country==country[i],c(1,3)]
#Make the table
data<-(table(a[, c("Appliance", "Year")]))
#Export the table as a CSV file, named according to each country.
write.csv(data,paste(country[i],"csv",sep = "."))
}