Re: [R] indexing question
idx = c(31, 36, 41, 61, 66, 71, 91, 96, 101, 121, 126, 131) temp = 1:150 res = temp[idx] dim(res) = c(3, length(res) %/% 3) res = t(rbind(res, res[2, ] - res[1, ], res[3, ] - res[2, ])) res Heikki Kaskelma -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Monday, November 13, 2006 10:06 PM To: r-help@stat.math.ethz.ch Subject: [R] indexing question I have the following set of indices, call it idx, that correspond to the indices of a vector say temp. [1] 31 36 41 61 66 71 91 96 101 121 126 131 151 156 161 181 186 191 211 216 221 241 246 251 271 276 281 301 306 311 331 336 341 361 366 [36] 371 391 396 401 421 426 431 451 456 461 481 486 491 511 516 521 541 546 551 571 576 581 601 606 611 631 636 641 661 666 671 691 696 701 721 [71] 726 731 751 756 761 781 786 791 811 816 821 841 846 851 871 876 881 901 906 911 931 936 941 961 966 971 991 996 1001 1021 1026 1031 1051 1056 1061 [106] 1081 1086 1091 1116 1121 1141 1146 1151 1171 1176 1181 1201 1206 1211 1231 1236 1241 1261 1266 1271 1291 1296 1301 1321 1326 1331 1351 1356 1361 1381 1386 1391 1411 1416 [141] 1421 I want to calculate temp[36] - temp[31] and temp[41] - temp[36] Similarly, temp[66] - temp[61] and temp[71] - temp[66] . . . . Similarly temp[1416]-temp[1411] temp[1421] - temp[1416] I'm doing this because the above subractions represent pairs of returns and the correlations between them wil be calculated eventually. In other words, eventually I will have X_36_31 ( i.e : temp[36] - temp[31] ) X_66-61 X_96-91 . . . . . . . X_1411-1416 as one vector and Y_41-36 Y_71-66 Y_101-96 . . . . . Y_1416_1421 as another vector. and will calculate the correlation between the two vectors in order to get one number. The point is I am really only using the indices 31, 61, 91 etc as anchor's so a regular diff(temp[idx]) won't work because it will diff all the elements that are next to each other ? This is a weird problem. I'm still thinking about it. I'm hoping to figure it out before someone sends me something but I won't mind so much if I get an external solution first. I have no pride. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] indexing question
On Tuesday 14 November 2006 09:28, Leeds, Mark (IED) wrote: > thanks beilton but that won't work. A diff will also include 61-41 etc > and I don't want to include those. > > I'm working on using lapply or sapply with a seq along 31, 61, etc. > I'll let you know if it works. > Try looking at: dim(idx) <- c(3, length(idx)/3) then: tmp[idx[2, ]] - temp[idx[1, ]] temp[idx[3, ]] - temp[idx[2, ]] HTH Ray Brownrigg __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] indexing question
thanks beilton but that won't work. A diff will also include 61-41 etc and I don't want to include those. I'm working on using lapply or sapply with a seq along 31, 61, etc. I'll let you know if it works. -Original Message- From: Benilton Carvalho [mailto:[EMAIL PROTECTED] Sent: Monday, November 13, 2006 3:18 PM To: Leeds, Mark (IED) Cc: r-help@stat.math.ethz.ch Subject: Re: [R] indexing question diff(tmp[idx]) cheers, b On Nov 13, 2006, at 3:06 PM, Leeds, Mark ((IED)) wrote: > I have the following set of indices, call it idx, that correspond to > the indices of a vector say temp. > > [1] 31 36 41 61 66 71 91 96 101 121 126 131 151 > 156 161 181 186 191 211 216 221 241 246 251 271 276 281 > 301 306 311 331 336 341 361 366 [36] 371 391 396 401 421 > 426 431 451 456 461 481 486 491 > 511 516 521 541 546 551 571 576 581 601 606 611 631 636 > 641 661 666 671 691 696 701 721 [71] 726 731 751 756 761 > 781 786 791 811 816 821 841 846 > 851 871 876 881 901 906 911 931 936 941 961 966 971 991 > 996 1001 1021 1026 1031 1051 1056 1061 [106] 1081 1086 1091 1116 > 1121 1141 1146 1151 1171 1176 1181 1201 > 1206 1211 1231 1236 1241 1261 1266 1271 1291 1296 1301 1321 1326 1331 > 1351 1356 1361 1381 1386 1391 1411 1416 [141] 1421 > > > I want to calculate temp[36] - temp[31] and temp[41] - temp[36] > > Similarly, temp[66] - temp[61] and temp[71] - temp[66] . > . > . > . > Similarly temp[1416]-temp[1411] > temp[1421] - temp[1416] > > > I'm doing this because the above subractions represent pairs of > returns > and the correlations between them wil be calculated eventually. > > In other words, eventually I will have > > X_36_31 ( i.e : temp[36] - temp[31] ) > X_66-61 > X_96-91 > . > . > . > . > . > . > . > X_1411-1416 > > as one vector and > > Y_41-36 > Y_71-66 > Y_101-96 > . > . > . > . > . > Y_1416_1421 > > as another vector. > > and will calculate the correlation between the two vectors in order to > get one number. > > > The point is I am really only using the indices 31, 61, 91 etc as > anchor's so a regular diff(temp[idx]) won't work because it will diff > all > the elements that are next to each other ? This is a weird problem. > I'm > still thinking about it. I'm hoping to figure it out before someone > sends me something but I won't mind so much if I get an external > solution first. I have no pride. > > > This is not an offer (or solicitation of an offer) to buy/se... > {{dropped}} > > __ > 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 > and provide commented, minimal, self-contained, reproducible code. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] indexing question
diff(tmp[idx]) cheers, b On Nov 13, 2006, at 3:06 PM, Leeds, Mark ((IED)) wrote: > I have the following set of indices, call it idx, that correspond > to the > indices of a vector say temp. > > [1] 31 36 41 61 66 71 91 96 101 121 126 131 151 > 156 161 181 186 191 211 216 221 241 246 251 271 276 281 > 301 306 311 331 336 341 361 366 > [36] 371 391 396 401 421 426 431 451 456 461 481 486 491 > 511 516 521 541 546 551 571 576 581 601 606 611 631 636 > 641 661 666 671 691 696 701 721 > [71] 726 731 751 756 761 781 786 791 811 816 821 841 846 > 851 871 876 881 901 906 911 931 936 941 961 966 971 991 > 996 1001 1021 1026 1031 1051 1056 1061 > [106] 1081 1086 1091 1116 1121 1141 1146 1151 1171 1176 1181 1201 > 1206 1211 1231 1236 1241 1261 1266 1271 1291 1296 1301 1321 1326 1331 > 1351 1356 1361 1381 1386 1391 1411 1416 > [141] 1421 > > > I want to calculate temp[36] - temp[31] and temp[41] - temp[36] > > Similarly, temp[66] - temp[61] and temp[71] - temp[66] > . > . > . > . > Similarly temp[1416]-temp[1411] > temp[1421] - temp[1416] > > > I'm doing this because the above subractions represent pairs of > returns > and the correlations between them wil be calculated eventually. > > In other words, eventually I will have > > X_36_31 ( i.e : temp[36] - temp[31] ) > X_66-61 > X_96-91 > . > . > . > . > . > . > . > X_1411-1416 > > as one vector and > > Y_41-36 > Y_71-66 > Y_101-96 > . > . > . > . > . > Y_1416_1421 > > as another vector. > > and will calculate the correlation between the two vectors in order to > get one number. > > > The point is I am really only using the indices 31, 61, 91 etc as > anchor's so a regular diff(temp[idx]) won't work because it will diff > all > the elements that are next to each other ? This is a weird problem. > I'm > still thinking about it. I'm hoping to figure it out before someone > sends me something but I won't mind so much if I get an external > solution first. I have no pride. > > > This is not an offer (or solicitation of an offer) to buy/se... > {{dropped}} > > __ > 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 > and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.
RE: [R] indexing question
Many many thanks. merge is the function I needed. Regards, Adrian -Original Message- From: Sundar Dorai-Raj [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 19, 2004 6:54 PM To: Adrian Dusa Cc: [EMAIL PROTECTED] Subject: Re: [R] indexing question Adrian Dusa wrote: > Hi, > > I have a problem and a rather poor solution that I would like to > improve. There a 2 datasets with different number of cases like this: > > Dataset 'poploc'Dataset 'siruta' > > Case no. SIRUTA TYPECase no. SIRUTA TYPE > 110170 11017 3 > 210260 21020 5 > 342711 0 31026 4 > . . > 13000100234 0 . > 16000160241 3 > > I want to bring the TIP variable in the 'poploc' dataset according to > the > SIRUTA variable (which has unique codes for each case, in both datasests). > The resulting dataset 'poploc' should look like this: > > Case no. SIRUTA TYPE > 110173 > 210264 > 342711 3 > > 13000100234 5 > > My current solution involves a combination of FOR looping and > indexing, which > takes about 3 minutes to complete. > > for (i in 1:nrow(siruta)) > poploc$TIP[poploc$SIRUTA %in% siruta$SIRUTA[i]] <- siruta$TIP[i] > > I'm sure there are more clever solutions, any help appreciated. Thank > you! Adrian > > __ > [EMAIL PROTECTED] mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html I think you are looking for ?merge. # note that poploc has no `TYPE' column poploc <- data.frame(no = 1:3, SIRUTA = c(1017, 1026, 42711)) siruta <- data.frame(no = c(1:3, 16000), SIRUTA = c(1017, 1026, 42711, 160241), TYPE = c(3, 5, 4, 3)) merge(poploc, siruta) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] indexing question
Adrian Dusa wrote: Hi, I have a problem and a rather poor solution that I would like to improve. There a 2 datasets with different number of cases like this: Dataset ‘poploc’Dataset ‘siruta’ Case no. SIRUTA TYPECase no. SIRUTA TYPE 110170 110173 210260 210205 342711 0 310264 … … 13000100234 0 … 16000160241 3 I want to bring the TIP variable in the ‘poploc’ dataset according to the SIRUTA variable (which has unique codes for each case, in both datasests). The resulting dataset 'poploc' should look like this: Case no. SIRUTA TYPE 110173 210264 342711 3 13000100234 5 My current solution involves a combination of FOR looping and indexing, which takes about 3 minutes to complete. for (i in 1:nrow(siruta)) poploc$TIP[poploc$SIRUTA %in% siruta$SIRUTA[i]] <- siruta$TIP[i] I’m sure there are more clever solutions, any help appreciated. Thank you! Adrian __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html I think you are looking for ?merge. # note that poploc has no `TYPE' column poploc <- data.frame(no = 1:3, SIRUTA = c(1017, 1026, 42711)) siruta <- data.frame(no = c(1:3, 16000), SIRUTA = c(1017, 1026, 42711, 160241), TYPE = c(3, 5, 4, 3)) merge(poploc, siruta) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] indexing question
take a look at match or maybe merge >-Original Message- >From: Adrian Dusa [mailto:[EMAIL PROTECTED] >Sent: Wednesday, May 19, 2004 8:44 AM >To: [EMAIL PROTECTED] >Subject: [R] indexing question > > >Hi, > >I have a problem and a rather poor solution that I would like >to improve. >There a 2 datasets with different number of cases like this: > >Dataset 'poploc'Dataset 'siruta' > >Case no. SIRUTA TYPECase no. >SIRUTA TYPE >110170 1 >10173 >210260 2 >10205 >342711 0 3 >10264 >... ... >13000100234 0 ... >16000 >160241 3 > >I want to bring the TIP variable in the 'poploc' dataset >according to the >SIRUTA variable (which has unique codes for each case, in both >datasests). >The resulting dataset 'poploc' should look like this: > >Case no. SIRUTA TYPE >110173 >210264 >342711 3 >... >13000100234 5 > >My current solution involves a combination of FOR looping and >indexing, which >takes about 3 minutes to complete. > >for (i in 1:nrow(siruta)) >poploc$TIP[poploc$SIRUTA %in% siruta$SIRUTA[i]] <- siruta$TIP[i] > >I'm sure there are more clever solutions, any help >appreciated. Thank you! >Adrian > >__ >[EMAIL PROTECTED] mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Indexing Question
I think what you want is lapply(b, function(x) sample(na.omit(x), 2, replace = TRUE)) -roger Nathan Cooper wrote: I have an indexing question. I have a data set that looks like this: b [[1]] [1] 22 23 24 25 26 [[2]] [1] 6 7 8 9 NA etc. from [[1]] to [[1000]] Then I need to use the sample function to take two samples from b[[1]] to b[[1000]] each separately. I thought something like "sample(na.omit(b[[1:1000]]),2,replace=TRUE)" would work but it doesn't. Is there a way to index this properly?? Thanks, Nathan _ Add MSN 8 Internet Software to your existing Internet access and enjoy patented spam protection and more. Sign up now! __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Indexing Question
"Nathan Cooper" <[EMAIL PROTECTED]> writes: > I have an indexing question. I have a data set that looks like this: > > >b > [[1]] > [1] 22 23 24 25 26 > > [[2]] > [1] 6 7 8 9 NA > > etc. from [[1]] to [[1000]] > > Then I need to use the sample function to take two samples from b[[1]] > to b[[1000]] each separately. I thought something like > "sample(na.omit(b[[1:1000]]),2,replace=TRUE)" would work but it > doesn't. Is there a way to index this properly?? Thanks, Not really. I'd try lapply(lapply(b,na.omit),sample,2,replace=TRUE) or lapply(b,function(x)sample(na.omit(x),2,replace=TRUE)) -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help