2017-09-18 2 views
0

Ich habe die folgenden Datenrahmen, die Informationen über verschiedene Zustände enthält.Schreibe Datenrahmen zu mehreren csv in R

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: Ich möchte jede Stadt Informationen in einzelnen csv des schreiben. Und jeder csv sollte wie unten aussehen. fertige Ausgabe:

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 

Vielen Dank im Voraus!

+1

Mögliches Duplikat [Wie mehr, CSV-Dateien in R erstellen?] (Https://stackoverflow.com/questions/8126984/how-to-create-multiple-csv-files-in-r) –

+1

Sie haben keine 'state'-Spalte? – simone

Antwort

1
split_df = split(df, list(df$city)) 

for (city in names(split_df)) { 
    write.csv(split_df[[[city]], paste0(city, ".csv")) 
} 
+0

Danke @Scott Warchal! – User0590

-1

Sie können dies mit Base R oder mit dem dplyr-Paket tun.

dplyr Weg:

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) 

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

Sie können eine for-Schleife verwenden, wenn Sie anfangen, mehr Städte zu bekommen.

for (city in c('Albuquerque', 'Augusta', 'Atlanta')) { 
    write.csv(df[df$city == city, ], file = paste0(city, '.csv')) 
} 
+0

Ich habe mehr 100 Städte. – User0590

+0

Dann verwenden Sie eine for-Schleife: für (Stadt in c ('Albuquerque', 'Augusta', 'Atlanta')) { write.csv (df [df $ city == Stadt,], file = paste0 (Stadt, ' .csv ')) } – charlesdarwin

+0

ersetzen Sie die c (' Albuquerque ',' Augusta ',' Atlanta ') mit dem Vektor mit Ihren Städtenamen – charlesdarwin

1
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=".")) ) 
+0

vielleicht 'write.csv()' verwenden, um expliziter zu sein. –

0

Es gibt ein paar verschiedene Möglichkeiten, dies aber eine sehr schnelle Annäherung tut dies auf einmal für alle Städte zu tun ist, die Vorteile der apply Familie von Funktionen in der Basis R aufzunehmen - speziell 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" 
+0

Danke @dshkol! – User0590

Verwandte Themen