Re: [R] apologes if you already saw this :efficiency question

From: jim holtman <jholtman_at_gmail.com>
Date: Thu 06 Jul 2006 - 12:49:33 EST

Is this what you want to do?

> x <- data.frame(a=paste(letters[1:10], 1:10),
+ b=paste(letters[11:20], 1:10), c=paste(LETTERS[1:10], 1:10))
> x

      a b c
1 a 1 k 1 A 1
2 b 2 l 2 B 2
3 c 3 m 3 C 3
4 d 4 n 4 D 4
5 e 5 o 5 E 5
6 f 6 p 6 F 6
7 g 7 q 7 G 7
8 h 8 r 8 H 8
9 i 9 s 9 I 9
10 j 10 t 10 J 10
> (y <- as.vector(t(x[,1:2])))
 [1] "a 1" "k 1" "b 2" "l 2" "c 3" "m 3" "d 4" "n 4" "e 5" "o 5" "f 6" "p 6" "g 7" "q 7"
[15] "h 8" "r 8" "i 9" "s 9" "j 10" "t 10"
> gsub(" ", "", y)

 [1] "a1" "k1" "b2" "l2" "c3" "m3" "d4" "n4" "e5" "o5" "f6" "p6" "g7" "q7" "h8" "r8"
[17] "i9" "s9" "j10" "t10"
>

On 7/5/06, markleeds@verizon.net <markleeds@verizon.net> wrote:
>
> hi everyone : i'm not sure if my previous mail about
> this got sent. i was typing and
> erroneosuyl hit a button and lost what i was typing.
>
> anyway, i have the code below ( it works ) in which i run through the rows
> of a dataframe, taking out the first two
> fields which are characters strings ( with some extra spacing so
> i yuse gsub) and appending these character strings to a list so that i can
> build one big list.
>
> there are 17,000 rows so i was hoping there might be a ( even just
> slightly. it doesn't have to be incresible improvement ) more efficient way
> to do this. I also think that remember someone saying that using the c
> command to make something bigger is not a good idea.
>
> the code is below. thanks.
>
> for paircounter in 1:nrow(tempdata) {
>
> firsstock<-gsub(" ","",tempdata[paircounter,1]
> secondstock<-gsub(" ","",tempdata[paircounter,2]
>
> if ( paircounter == 1 ) {
> stocklist<-c(firststock,secondstock)
> } else {
> stocklist<(stocklist,firststock,secondstock)
> }
> }
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Jul 06 14:03:48 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 06 Jul 2006 - 16:14:32 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.