How to save output of for loop in separate csv files?

时间秒杀一切 提交于 2020-03-21 06:22:52

问题


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

reproducible example data:

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))

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")

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'.

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")} 

enter image description here


回答1:


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)
}

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)
})

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)
})


来源:https://stackoverflow.com/questions/45656311/how-to-save-output-of-for-loop-in-separate-csv-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!