Dear All, I would like to to group the Ticker by Industry and create file names from the Industry Factor and export to a txt file. I have tried the folowing ind=finvizAllexETF$Industry ind is then "Aluminum" "Business Services" "Regional Airlines" ind2=gsub(" " ,"",ind) ind3 [1] "Aluminum" "BusinessServices" "RegionalAirlines"> for (i in 1:3) ind3[i]<- AllexETF$Ticker[AllexETF$Industry==ind2[i]]Warning messages: 1: In ind3[i] <- finvizAllexETF$Ticker[AllexETF$Industry == ind2[i]] : number of items to replace is not a multiple of replacement length> str(AllexETF)'data.frame': 5137 obs. of 11 variables: $ No. : int 1 2 3 4 5 6 7 8 9 10 ... $ Ticker : Factor w/ 5137 levels "A","AA","AAC",..: 1 2 3 4 5 6 7 8 9 10 ... $ Company : Factor w/ 5130 levels "012 Smile.Communications Ltd.",..: 127 158 33 437 141 148 459 25 23 87 ... $ Sector : Factor w/ 9 levels "Basic Materials",..: 8 1 7 4 7 6 4 7 6 7 ... $ Industry : Factor w/ 212 levels "Accident & Health Insurance",..: 175 8 27 43 160 4 105 168 77 16 ... $ Country : Factor w/ 51 levels "Argentina","Australia",..: 51 51 8 51 51 51 51 51 51 51 ... $ Market.Cap: num 10614.9 15229.56 6.35 185.38 734.64 ... $ P.E : num NA NA NA 24.2 NA ... $ Price : num 30.43 15.63 0.78 6.06 5.46 ... $ Change : Factor w/ 1119 levels "","-0.01%","-0.03%",..: 250 114 645 573 109 645 379 10 402 63 ... $ Volume : int 3309434 33389060 7600 42396 4406265 0 4837 447195 75997 738875 ... head(AllexETF) No. Ticker Company Sector Industry Country Market.Cap P.E Price Change Volume 1 1 A Agilent Technologies Inc. Technology Scientific & Technical Instruments USA 10614.90 NA 30.43 -2.31% 3309434 2 2 AA Alcoa, Inc. Basic Materials Aluminum USA 15229.56 NA 15.63 -1.14% 33389060 3 3 AAC Ableauctions.com Inc. Services Business Services Canada 6.35 NA 0.78 0.00% 7600 4 4 AACC Asset Acceptance Capital Corp. Financial Credit Services USA 185.38 24.24 6.06 -6.34% 42396 5 5 AAI AirTran Holdings Inc. Services Regional Airlines USA 734.64 NA 5.46 -1.09% 4406265 6 6 AAII Alabama Aircraft Industries, Inc Industrial Goods Aerospace/Defense Products & Services USA 5.58 NA 1.35 0.00% 0 Thanks in advance, Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1018249.html Sent from the R help mailing list archive at Nabble.com.
Peter Rote wrote:> > I would like to to group the Ticker by Industry and create file names from > the > Industry Factor and export to a txt file. > > I have tried the folowing > > ind=finvizAllexETF$Industry > > ind is then "Aluminum" "Business Services" "Regional Airlines" > > ind2=gsub(" " ,"",ind) > ind3 > [1] "Aluminum" "BusinessServices" "RegionalAirlines" > >> for (i in 1:3) ind3[i]<- AllexETF$Ticker[AllexETF$Industry==ind2[i]] > > Warning messages: > 1: In ind3[i] <- finvizAllexETF$Ticker[AllexETF$Industry == ind2[i]] : > number of items to replace is not a multiple of replacement length > >If this happens, try to do a finvizAllexETF$Ticker[AllexETF$Industry == ind2[i]] You will note that it returns not one, but many items, and assigning it to ind[i] will fail. Sometimes, it helps to add a [1] at the end, but there is another problem that these are factors and you want strings. The example below shows on method: set.seed(4711) AlexETF = data.frame(Industry=sample(c("Business Services", "Aluminium","Regional Airlines"),10,TRUE),Price = rnorm(10,10)) by(AlexETF,AlexETF$Industry,function(a) { filename = paste(gsub(" ","",a$Industry[1]),".txt",sep="") print(filename) write.table(a,file=filename) } ) Dieter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1018269.html Sent from the R help mailing list archive at Nabble.com.
by the way how do i change the output "1016" "Advertising Agencies" "CMM" "1803" "Advertising Agencies" "FMCN" "2427" "Advertising Agencies" "IPG" "3093" "Advertising Agencies" "MWW" "3372" "Advertising Agencies" "OMC" "4809" "Advertising Agencies" "VCLK" "4832" "Advertising Agencies" "VISN" "5005" "Advertising Agencies" "WPPGY" "5089" "Advertising Agencies" "XSEL" to just CMM FMCN IPG MWW Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1032753.html Sent from the R help mailing list archive at Nabble.com.
Thank you Dieter and Rolf, I have solved the slash Problem, but I still struggling with the output files. I have tried this by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" ","",a$Industry[1]),".txt",sep="") print(filename) write.table(a,file=filename,col.names = FALSE) } ) and this by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" ","",a$Industry[1]),".txt",sep="") print(filename) write(as.character(a),file=filename) } ) I want in each file just the ticker with out any quotations mark. CMM FMCN IPG MWW Thanks in advance, Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1073567.html Sent from the R help mailing list archive at Nabble.com.
Does this example help?> a <- matrix(letters[1:12], ncol=3) > a[,1] [,2] [,3] [1,] "a" "e" "i" [2,] "b" "f" "j" [3,] "c" "g" "k" [4,] "d" "h" "l"> write.table(a[,3,drop=FALSE],quote=FALSE,col.names=FALSE,row.names=FALSE)i j k l At 4:11 PM -0800 1/21/10, Peter Rote wrote:>Thank you Dieter and Rolf, > >I have solved the slash Problem, but I still struggling with the output >files. > >I have tried this > by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" >","",a$Industry[1]),".txt",sep="") > print(filename) > write.table(a,file=filename,col.names = FALSE) > } > ) > >and this > > by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" >","",a$Industry[1]),".txt",sep="") > print(filename) > write(as.character(a),file=filename) > } > ) > > >I want in each file just the ticker with out any quotations mark. > >CMM >FMCN >IPG >MWW > >Thanks in advance, >Peter > >-- >View this message in context: >http://*n4.nabble.com/Data-Manipulation-tp1018249p1073567.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
Thank you Don for the code, but I get the following error massage:> by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" > ","",a$Industry[1]),".txt",sep="")+ print(filename) + write.table(a[,3,drop=FALSE],quote=FALSE,col.names=FALSE,row.names=FALSE) + } + ) [1] "C:/ab/Accident&HealthInsurance.txt" Error in `[.data.frame`(a, , 3, drop = FALSE) : undefined columns selected Best, Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1100168.html Sent from the R help mailing list archive at Nabble.com.
I still struggling with this: error massage:> by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" > ","",a$Industry[1]),".txt",sep="")+ print(filename) + write.table(a[,3,drop=FALSE],quote=FALSE,col.names=FALSE,row.names=FALSE) + } + ) [1] "C:/ab/Accident&HealthInsurance.txt" Error in `[.data.frame`(a, , 3, drop = FALSE) : undefined columns selected Best, Peter -- View this message in context: http://n4.nabble.com/Data-Manipulation-tp1018249p1290191.html Sent from the R help mailing list archive at Nabble.com.
On 01/26/2010 09:15 PM, Peter Rote wrote:> > I still struggling with this: > > error massage: > >> by(AlexETF,AlexETF$Industry,function(a) {filename = paste("C:/ab/",gsub(" >> ","",a$Industry[1]),".txt",sep="") > + print(filename) > + write.table(a[,3,drop=FALSE],quote=FALSE,col.names=FALSE,row.names=FALSE) > + } > + ) > > [1] "C:/ab/Accident&HealthInsurance.txt" > Error in `[.data.frame`(a, , 3, drop = FALSE) : > undefined columns selectedHi Peter, I would suggest that you print the first say 10 rows of "a" and see if it has three columns. Jim