[R] Data Manipulation

Dieter Menne dieter.menne at menne-biomed.de
Wed Jan 20 10:29:10 CET 2010



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.



More information about the R-help mailing list