如何将for循环的输出保存在单独的csv文件中? [英] How to save output of for loop in separate csv files?

查看:508
本文介绍了如何将for循环的输出保存在单独的csv文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我在下面附加的图像和代码,我有一组交易数据,每一行都有其行业名称.

As an image and code that I attached below, I have a set of transaction data and each row has its industry name.

可复制的示例数据:

structure(list(Date = c(1201, 1201, 1201, 1201, 1201, 1201, 1201, 
1201, 1201, 1201), Sex = c("Male", "Male", "Female", "Male", 
"Male", "Female", "Male", "Female", "Male", "Male"), Age = c(10, 
15, 20, 15, 40, 50, 20, 30, 50, 20), City = c("Pheonix", "Atlanta", 
"Las Vegas", "Las Vegas", "Denver", "Pheonix", "Atlanta", "Las Vegas", 
"Las Vegas", "Minneapolis"), State = c("Arizona", "Georgia", 
"Nevada", "Nevada", "Colorado", "Arizona", "Georgia", "Nevada", 
"Nevada", "Minesota"), Industry = c("food", "furniture", "clothes", 
"transportation", "leisure", "food", "furniture", "food", "transportation", 
"furniture"), `no.of users` = c(48, 50, 83, 111, 186, 196.7, 
230.4, 264.1, 297.8, 331.5), `no. of approval cases` = c(48, 
21, 25, 48, 70, 63.7, 70.8, 77.9, 85, 92.1), `Total spending` = c(1541000, 
512000, 1757000, 1117000, 1740500, 1634700, 1735100, 1835500, 
1935900, 2036300)), .Names = c("Date", "Sex", "Age", "City", 
"State", "Industry", "no.of users", "no. of approval cases", 
"Total spending"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

我想做的是仅对行业名称与条件匹配的行进行子集化,并将其导出为单独的csv文件,其文件名与行业名称相对应. 例如,假设我有一个

What I would like to do is to subset only rows whose industry names match a condition and export it as separate csv files with the name of the file corresponding to its industry name. For example, let's say I have a list of

 current_industry<-c("food","furniture")

首先,我想对行业名称与"food"匹配的行进行子集,然后将其保存为csv files名称'food',然后对以下行业名称与"furniture"匹配的行进行子集设置,并将其另存为csv files名称为家具".

First, I would like to subset rows whose industry names match "food" and then save it to csv files name 'food', then subsetting the following rows whose industry name matches to "furniture" and save it as separate csv files name 'furniture'.

我编写了for循环来执行此操作,但是此操作无效,因为以下子设置数据替换了之前的子设置数据.

I wrote for-loop to do this, but this doesn't work as the following subsetting data replace the one beforehand.

for(i in current_industry){
  write.csv(subset(masterset, industry == i), "i.csv")} 

在此处输入图片描述

推荐答案

我认为这会起作用.运行此代码后,您将在工作目录中找到food.csvfurniture.csv.

I think this will work. After running this code, you will find food.csv and furniture.csv in your working directory.

current_industry <- c("food", "furniture")

for (i in current_industry){
  dt2 <- subset(dt, Industry %in% i)
  write.csv(dt2, paste0(i, ".csv"), row.names = FALSE)
}

更新

您还可以考虑以下内容.此代码会将所有行业另存为单独的CSV文件到您的工作目录中.

Update

You may also consider the following. This code will save all industry as separated CSV file in your working directory.

dt_list <- split(dt, f = dt$Industry)

lapply(dt_list, function(dt){
  write.csv(dt, paste0(unique(dt$Industry), ".csv"), row.names = FALSE)
})

并且以下内容将仅保存foodfurniture.关键是使用current_industry子集dt_list.

And the following will save only food and furniture. The key is to use current_industry to subset dt_list.

dt_list <- split(dt, f = dt$Industry)

dt_list2 <- dt_list[current_industry]

lapply(dt_list2, function(dt){
  write.csv(dt, paste0(unique(dt$Industry), ".csv"), row.names = FALSE)
})

这篇关于如何将for循环的输出保存在单独的csv文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆