-2

This is my dataframe

df_temp=data.frame(ID=c(1,1,2,3,3),City=c("a","a","b","c","c"),Qty=c(20,14,40,50,60))

I want to filter the data for unique city and export that data based on the filtered result with the name as the name of unique city so my that I'll have 3(a.csv, b.csv, c.csv) csv files with records

a.csv
ID  City Qty
1    a    20
1    a    14

b.csv
ID  City Qty
2    b    40

c.csv
ID  City Qty
3    c    50
3    c    60

This is what I have tried

name=as.character(unique(df_temp[,2]))
df_temp1=c()
for(i in df_temp[,2]){
paste("D:/Files",df_temp[i,2])
df_temp1[i]=filter(df_temp, City %in% c("a"))
write.csv("D/:Files",paste("D:/Files",name[i],".csv",sep = ""))
}

But That gives my "FileNa.csv" with empty rows. Any help would be much appreciated..Thanks

Andre_k
  • 1,680
  • 3
  • 18
  • 41

2 Answers2

2
  1. Check the correct arguments with ?write.csv: write.table(x, file = "").

  2. If I do not misunderstand, the idea should be to create the data frames first without necessarily including their paths for the filenames.

  3. I guess you want to cycle through the unique city names, and not for every city name in the for() loop.

  4. Use well-formatted code in your next questions, please (with spaces -- you could also consult Google's R Style Guide).

  5. Why do you use = instead of <-? Why do you use %in% ? Why do you use filter() instead of subset()? Please consult the relevant function documentations (e.g.: ?filter shows that this is not an appropriate function for what you want to do!)

The following stores each dataframe as an element of a list, for each city name. It also saves a csv file for each data frame (each dataframe corresponds to a different city).

dataframe_temp = data.frame(ID = c(1, 1, 2, 3, 3), 
                            City = c("a", "a", "b", "c", "c"), 
                            Qty = c(20, 14, 40, 50, 60))

dataframe.list <- list()

# avoid hardcoded numbers as ", 2" as theses can 
# change in the future, after e.g. an expansion of the dataframe!
for (city in unique(dataframe_temp$City)) { 

    print(city)

    dataframe.list[[i]] = subset(dataframe_temp, City == city) 

    # use your folder location instead of "~/"
    write.csv(x = dataframe.list[[i]], file = paste0("~/", "dataframe_for_city_", city,".csv")) 
}

For net stumblers, I expand the answer of emilliman5 for larger (think of writing multi-GB a lot faster) datasets with data.table:

library(data.table)
DT <- data.table(ID = c(1, 1, 2, 3, 3), 
                        City = c("a", "a", "b", "c", "c"), 
                        Qty = c(20, 14, 40, 50, 60))
# setkeyv(DT, 'City') # check if you really need a key: https://stackoverflow.com/questions/20039335/what-is-the-purpose-of-setting-a-key-in-data-table
invisible(lapply(unique(DT[, City]), function(city) 
          fwrite(x = subset(DT, City == city), 
              file = paste0("~/", "DT_for_city_", city,".csv"))))
Konstantinos
  • 4,096
  • 3
  • 19
  • 28
1

How about something like this:

df_temp=data.frame(ID=c(1,1,2,3,3),City=c("a","a","b","c","c"),Qty=c(20,14,40,50,60))

cities <- split(df_temp, df_temp$City)

lapply(cities, function(x) write.csv(x, file=paste0("D:/Files/City_", x[1,"City"], ".txt")))
emilliman5
  • 5,816
  • 3
  • 27
  • 37