Thank you Dieter, but i still have a problem to write to file. The problem is the slash in file names (Aerospace/Defense Products & Services ). If i want it to C:/ab/ so "C:/ab/AdvertisingAgencies.txt" is ok but "C:/ab/Aerospace/Defense-MajorDiversified.txt" is not
> head(AlexETF) AlexETF.Industry AlexETF.Ticker 1 Scientific & Technical Instruments A 2 Aluminum AA 3 Business Services AAC 4 Credit Services AACC 5 Regional Airlines AAI 6 Aerospace/Defense Products & Services AAII > by(AlexETF,AlexETF$Industry,function(a) {filename = paste(gsub(" > ","",a$Industry[1]),".txt",sep="") + print(filename) + + } + ) [1] "Accident&HealthInsurance.txt" [1] "AdvertisingAgencies.txt" [1] "Aerospace/Defense-MajorDiversified.txt" [1] "Aerospace/DefenseProducts&Services.txt" [1] "AgriculturalChemicals.txt" [1] "AirDelivery&FreightServices.txt" > by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" > ","",a$Industry[1]),".txt",sep="") + write.table(a,file=filename,col.names = FALSE) + } + ) Error in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file 'C:/ab/Aerospace/Defense-MajorDiversified.txt': No such file or directory Thanks in advance, Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1032029.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.