Reputation: 103
I have the below data frame which contains infomation about different states.
long=c(-106.61291,-106.61291,-106.61291,-81.97224,-81.97224,-81.97224,-84.4277,-84.4277,-84.4277)
lat=c(35.04333,35.04333,35.04333,33.37378,33.37378,33.37378,33.64073,33.64073,33.64073)
city=c("Albuquerque","Albuquerque","Albuquerque","Augusta","Augusta","Augusta","Atlanta","Atlanta","Atlanta")
date=c("2017-08-22","2017-08-23","2017-09-24","2017-09-28","2017-10-24","2017-09-22","2017-11-12","2017-010-14","2017-09-03")
value=c(12,10.8,18.3,12.4,43,21,12,32.1,14)
df<-data.frame(long,lat,city,date,value)
Problem:I want to write each city information in individual csv's. And each csv should look like below. Final output:
Albuquerque.csv
long lat city date value
1 -106.6129 35.04333 Albuquerque 2017-08-22 12.0
2 -106.6129 35.04333 Albuquerque 2017-08-23 10.8
3 -106.6129 35.04333 Albuquerque 2017-09-24 18.3
Augusta.csv
long lat city date value
1 -81.97224 33.37378 Augusta 2017-09-28 12.4
2 -81.97224 33.37378 Augusta 2017-10-24 43.0
3 -81.97224 33.37378 Augusta 2017-09-22 21.0
Atlanta.csv
long lat city date value
1 -84.4277 33.64073 Atlanta 2017-11-12 12.0
2 -84.4277 33.64073 Atlanta 2017-010-14 32.1
3 -84.4277 33.64073 Atlanta 2017-09-03 14.0
Thanks in advance!
Upvotes: 1
Views: 2571
Reputation: 1027
# Split dataframe by city
split_df <- split(df, list(df$city))
# Write out separate CSV for each city
for (city in names(split_df)) {
write.csv(split_df[[city]], paste0(city, ".csv"))
}
Upvotes: 3
Reputation: 577
long=c(-106.61291,-106.61291,-106.61291,-81.97224,-81.97224,-81.97224,-84.4277,-84.4277,-84.4277)
lat=c(35.04333,35.04333,35.04333,33.37378,33.37378,33.37378,33.64073,33.64073,33.64073)
city=c("Albuquerque","Albuquerque","Albuquerque","Augusta","Augusta","Augusta","Atlanta","Atlanta","Atlanta")
date=c("2017-08-22","2017-08-23","2017-09-24","2017-09-28","2017-10-24","2017-09-22","2017-11-12","2017-010-14","2017-09-03")
value=c(12,10.8,18.3,12.4,43,21,12,32.1,14)
df<-data.frame(long,lat,city,date,value)
dflist <- split(df , f = df$city)
sapply(names(dflist),
function (x) write.csv(dflist[[x]], file=paste(x, "csv", sep=".") ) )
Upvotes: 1
Reputation: 767
You can do this with base R or with the dplyr package.
dplyr way:
dplyr::filter(df, city == 'Albuquerque') %>% write.csv(file = 'Albuquerque.csv', row.names = FALSE)
dplyr::filter(df, city == 'Augusta') %>% write.csv(file = 'Augusta.csv', row.names = FALSE)
dplyr::filter(df, city == 'Atlanta') %>% write.csv(file = 'Atlanta.csv', row.names = FALSE)
base R:
write.csv(df[df$city == 'Albuquerque', ], file = 'Albuquerque.csv', row.names = FALSE)
write.csv(df[df$city == 'Augusta', ], file = 'Augusta.csv', row.names = FALSE)
write.csv(df[df$city == 'Atlanta', ], file = 'Atlanta.csv', row.names = FALSE)
You can use a for loop if you start getting more cities.
for (city in c('Albuquerque', 'Augusta', 'Atlanta')) {
write.csv(df[df$city == city, ], file = paste0(city, '.csv'))
}
Upvotes: -1
Reputation: 1228
There's a few different ways of doing this but a very quick approach to do this for all your cities at once is to take advantage of the apply
family of functions in base R - specifically lapply
.
long=c(-106.61291,-106.61291,-106.61291,-81.97224,-81.97224,-81.97224,-84.4277,-84.4277,-84.4277)
lat=c(35.04333,35.04333,35.04333,33.37378,33.37378,33.37378,33.64073,33.64073,33.64073)
city=c("Albuquerque","Albuquerque","Albuquerque","Augusta","Augusta","Augusta","Atlanta","Atlanta","Atlanta")
date=c("2017-08-22","2017-08-23","2017-09-24","2017-09-28","2017-10-24","2017-09-22","2017-11-12","2017-010-14","2017-09-03")
value=c(12,10.8,18.3,12.4,43,21,12,32.1,14)
df<-data.frame(long,lat,city,date,value)
# create a convenience function to split your data and export to csv
split_into_csv <- function(x) {
tmp <- df[df$city == x,]
write.csv(tmp, file = paste0(x,".csv"))}
# Apply split_into_csv over elements of list with lapply
lapply(levels(df$city), split_into_csv)
# Check output in director
dir()
[1] "Albuquerque.csv" "Atlanta.csv" "Augusta.csv"
Upvotes: 0