Skip to content

Data Manipulation

12 messages · Dieter Menne, Peter Rote, Rolf Turner +3 more

#
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"
Warning messages:
1: In ind3[i] <- finvizAllexETF$Ticker[AllexETF$Industry == ind2[i]] :
  number of items to replace is not a multiple of replacement length
'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
#
Peter Rote wrote:
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
#
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
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
+      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"
+      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
#
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
#
A name such as ``Aerospace/Defense etc.'' is certainly not a legal
file name under unix-alike systems, and I suspect it would not
be even under Windoze.  Even if it is, you shouldn't use it!

Change the name to ``Aerospace-Defense Products & Services''
or something like that, for goodness sake.

	cheers,

		Rolf Turner
On 21/01/2010, at 2:19 PM, Peter Rote wrote:

            
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
Peter Rote wrote:
As Rolf said, the slash is not legal in a file name, it is treated like a
backslash ("\\") when run under Windows. Use create.dir to created
Aerospace, or change the slash to something else.

Dieter
#
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
#
Does this example help?
[,1] [,2] [,3]
[1,] "a"  "e"  "i"
[2,] "b"  "f"  "j"
[3,] "c"  "g"  "k"
[4,] "d"  "h"  "l"
i
j
k
l
At 4:11 PM -0800 1/21/10, Peter Rote wrote:

  
    
#
Thank you Don for the code, 

but I get the following error massage:
+         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
3 days later
#
I still struggling with this:

 error massage:
+         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
#
Peter Rote wrote:
The message says that you haven't got three columns in a, so try
inserting print(dim(a)). Perhaps what you showed earlier was rownames
plus two columns?
#
On 01/26/2010 09:15 PM, Peter Rote wrote:
Hi Peter,
I would suggest that you print the first say 10 rows of "a" and see if 
it has three columns.

Jim