Re: [R] Order sapply
Good Afternoon, I get it, my question was really how the data were organized, I thought I was doing something wrong. Thanks -- View this message in context: http://r.789695.n4.nabble.com/Order-sapply-tp4537496p4542518.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Order sapply
Hello, > >I have the following code, but it seems that something must be doing > wrong, because it is giving the results I want. > The idea is to create segments while the value of Commutation is less than > 1000. > for example, from the small set of data below > I believe that code was mine, from an answer to a post of yours. > > I think there is a problem in the order of the different blocks, I don’t > understand, how is that data are organized. > The idea is to keep the organization of the file near the original. > Maybe that's not possible. The order is given by 'split'. But you can make it "near the original", if you put comm1000 last in the split list. # This line should be changed # result <- split(df1[!inx, ], list(comm1000[!inx], df1$v_source[!inx], df1$v_destine[!inx])) # to this one. result <- split(df1[!inx, ], list(df1$v_source[!inx], df1$v_destine[!inx], comm1000[!inx])) You can also use Sarah's suggestion and have the entire results data frame ordered inx_ord <- with(results_user, order(user, v_source, v_destine)) results_user[inx_ord, ] Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Order-sapply-tp4537496p4539748.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Order sapply
On Fri, Apr 6, 2012 at 10:27 AM, MSousa wrote: > Good Afternoon, > > I have the following code, but it seems that something must be doing > wrong, because it is giving the results I want. Assuming you don't really mean that, you can use order() and/or sort() to put it back into order by val_user and pos. If you provide reproducible data with dput(), someone might be inspired to write you the code to do so. Sarah > The idea is to create segments while the value of Commutation is less than > 1000. > for example, from the small set of data below > > text=" > val_user pos v v_star v_end commutation v_source > v_destine > 1 1 96-96 1173438391 1173438391 0 96 96 > 3 2 126-126 1172501729 1172501532 197 126 126 > 3 3 126-35 1174404177 1172501909 1902268 126 35 > 3 4 35-56 1174404252 1174404221 31 35 56 > 3 5 56-99 1174404295 1174404295 0 56 99 > 3 6 99-92 1174404536 1174404535 1 99 92 > 3 7 92-99 1174404660 1174404658 2 92 99 > 3 8 99-43 1174405442 1174405442 0 99 43 > 3 9 43-99 1174405545 1174405544 1 43 99 > 3 10 99-43 1174405581 1174405581 0 99 43 > 3 11 43-99 1174405836 1174405836 0 43 99 > 3 12 99-43 1174405861 1174405861 0 99 43 > 3 13 43-99 1174405875 1174405875 0 43 99 > 3 18 101-113 1174410215 1174410214 1 101 113 > 3 19 113-36 1174410261 1174410261 0 113 36 > 3 20 36-60 1174410268 1174410268 0 36 60 > 3 21 60-101 1174660357 1174411020 249337 60 101 > 3 22 101-191 1174666205 1174662119 4086 101 191 > 3 23 191-196 1174666278 1174666265 13 191 196 > 3 24 196-9 1174666398 1174666366 32 196 9 > 3 25 9-101 1175154139 1174667144 486995 9 101 > 3 26 101-37 1175160182 1175159734 448 101 37 > 3 27 37-55 1175160256 1175160257 -1 37 55 > 4 1 11-11 1216304836 1216304127 709 11 11 > 4 2 11-11 1216370154 1216312995 57159 11 11 > 4 3 11-11 1216373234 1216372799 435 11 11 > 4 4 11-11 1216373974 1216373373 601 11 11 > 4 5 11-11 1216382659 1216379277 3382 11 11 > 4 6 11-11 1216397081 1216395201 1880 11 11 > 4 7 11-11 1216397339 1216397131 208 11 11 > 4 8 11-11 1216630649 1216399235 231414 11 11 > 4 9 11-11 1216637080 1216631541 5539 11 11 > 4 10 11-11 1216646563 1216640763 5800 11 11 > 4 11 11-11 1216656338 1216651635 4703 11 11 > " > df1 <-read.table(textConnection(text), header=TRUE) > > inx <- df1$commutation > 1000 > comm1000 <- cumsum(inx) > > result <- split(df1[!inx, ], list(comm1000[!inx], df1$v_source[!inx], > df1$v_destine[!inx])) > result <- sapply(result, function(x) c(x$val_user[1], x$v_source[1], > x$v_destine[1], nrow(x), mean(x$comm))) > result <- na.exclude(t(result)) > > rownames(result) <- 1:nrow(result) > colnames(result) <- c("user", "v_source", "v_destine", "count", "average") > attr(result, "na.action") <- NULL > attr(result, "class") <- NULL > results_user<-data.frame(result) > View(results_user) > > This give: > user v_source v_destine count Min Max average > > > but the results I want: > user v_source v_destine count Min Max average > 1 96 96 1 0 0 0.000 > 3 126 126 1 197 197 197.000 > 3 35 56 1 31 31 31.000 > …. > > > I think there is a problem in the order of the different blocks, I don’t > understand, how is that data are organized. > The idea is to keep the organization of the file near the original. > > Thanks > -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] Order sapply
On Fri, Apr 6, 2012 at 10:27 AM, MSousa wrote: > Good Afternoon, > > I have the following code, but it seems that something must be doing > wrong, because it is giving the results I want. Didn't someone else have that problem just a few weeks ago? :-P Michael > The idea is to create segments while the value of Commutation is less than > 1000. > for example, from the small set of data below > > text=" > val_user pos v v_star v_end commutation v_source > v_destine > 1 1 96-96 1173438391 1173438391 0 96 96 > 3 2 126-126 1172501729 1172501532 197 126 126 > 3 3 126-35 1174404177 1172501909 1902268 126 35 > 3 4 35-56 1174404252 1174404221 31 35 56 > 3 5 56-99 1174404295 1174404295 0 56 99 > 3 6 99-92 1174404536 1174404535 1 99 92 > 3 7 92-99 1174404660 1174404658 2 92 99 > 3 8 99-43 1174405442 1174405442 0 99 43 > 3 9 43-99 1174405545 1174405544 1 43 99 > 3 10 99-43 1174405581 1174405581 0 99 43 > 3 11 43-99 1174405836 1174405836 0 43 99 > 3 12 99-43 1174405861 1174405861 0 99 43 > 3 13 43-99 1174405875 1174405875 0 43 99 > 3 18 101-113 1174410215 1174410214 1 101 113 > 3 19 113-36 1174410261 1174410261 0 113 36 > 3 20 36-60 1174410268 1174410268 0 36 60 > 3 21 60-101 1174660357 1174411020 249337 60 101 > 3 22 101-191 1174666205 1174662119 4086 101 191 > 3 23 191-196 1174666278 1174666265 13 191 196 > 3 24 196-9 1174666398 1174666366 32 196 9 > 3 25 9-101 1175154139 1174667144 486995 9 101 > 3 26 101-37 1175160182 1175159734 448 101 37 > 3 27 37-55 1175160256 1175160257 -1 37 55 > 4 1 11-11 1216304836 1216304127 709 11 11 > 4 2 11-11 1216370154 1216312995 57159 11 11 > 4 3 11-11 1216373234 1216372799 435 11 11 > 4 4 11-11 1216373974 1216373373 601 11 11 > 4 5 11-11 1216382659 1216379277 3382 11 11 > 4 6 11-11 1216397081 1216395201 1880 11 11 > 4 7 11-11 1216397339 1216397131 208 11 11 > 4 8 11-11 1216630649 1216399235 231414 11 11 > 4 9 11-11 1216637080 1216631541 5539 11 11 > 4 10 11-11 1216646563 1216640763 5800 11 11 > 4 11 11-11 1216656338 1216651635 4703 11 11 > " > df1 <-read.table(textConnection(text), header=TRUE) > > inx <- df1$commutation > 1000 > comm1000 <- cumsum(inx) > > result <- split(df1[!inx, ], list(comm1000[!inx], df1$v_source[!inx], > df1$v_destine[!inx])) > result <- sapply(result, function(x) c(x$val_user[1], x$v_source[1], > x$v_destine[1], nrow(x), mean(x$comm))) > result <- na.exclude(t(result)) > > rownames(result) <- 1:nrow(result) > colnames(result) <- c("user", "v_source", "v_destine", "count", "average") > attr(result, "na.action") <- NULL > attr(result, "class") <- NULL > results_user<-data.frame(result) > View(results_user) > > This give: > user v_source v_destine count Min Max average > > > but the results I want: > user v_source v_destine count Min Max average > 1 96 96 1 0 0 0.000 > 3 126 126 1 197 197 197.000 > 3 35 56 1 31 31 31.000 > …. > > > I think there is a problem in the order of the different blocks, I don’t > understand, how is that data are organized. > The idea is to keep the organization of the file near the original. > > Thanks > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Order-sapply-tp4537496p4537496.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. __ R-help@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.
[R] Order sapply
Good Afternoon, I have the following code, but it seems that something must be doing wrong, because it is giving the results I want. The idea is to create segments while the value of Commutation is less than 1000. for example, from the small set of data below text=" val_user posvv_starv_endcommutationv_source v_destine 11 96-961173438391117343839109696 32126-12611725017291172501532197126126 33126-3511744041771172501909190226812635 3435-5611744042521174404221313556 3556-991174404295117440429505699 3699-921174404536117440453519992 3792-991174404660117440465829299 3899-431174405442117440544209943 3943-991174405545117440554414399 31099-431174405581117440558109943 31143-991174405836117440583604399 31299-431174405861117440586109943 31343-991174405875117440587504399 318101-113117441021511744102141101113 319113-3611744102611174410261011336 32036-601174410268117441026803660 32160-1011174660357117441102024933760101 322101-191117466620511746621194086101191 323191-1961174666278117466626513191196 324196-911746663981174666366321969 3259-101117515413911746671444869959101 326101-371175160182117515973444810137 32737-5511751602561175160257-13755 4111-11121630483612163041277091111 4211-1112163701541216312995571591111 4311-11121637323412163727994351111 4411-11121637397412163733736011111 4511-111216382659121637927733821111 4611-111216397081121639520118801111 4711-11121639733912163971312081111 4811-11121663064912163992352314141111 4911-111216637080121663154155391111 41011-111216646563121664076358001111 41111-111216656338121665163547031111 " df1 <-read.table(textConnection(text), header=TRUE) inx <- df1$commutation > 1000 comm1000 <- cumsum(inx) result <- split(df1[!inx, ], list(comm1000[!inx], df1$v_source[!inx], df1$v_destine[!inx])) result <- sapply(result, function(x) c(x$val_user[1], x$v_source[1], x$v_destine[1], nrow(x), mean(x$comm))) result <- na.exclude(t(result)) rownames(result) <- 1:nrow(result) colnames(result) <- c("user", "v_source", "v_destine", "count", "average") attr(result, "na.action") <- NULL attr(result, "class") <- NULL results_user<-data.frame(result) View(results_user) This give: user v_source v_destine count Min Max average but the results I want: user v_source v_destine count Min Max average 1 96 96 1 0 0 0.000 3 126 126 1 197 197 197.000 3 35 56 1 31 31 31.000 …. I think there is a problem in the order of the different blocks, I don’t understand, how is that data are organized. The idea is to keep the organization of the file near the original. Thanks -- View this message in context: http://r.789695.n4.nabble.com/Order-sapply-tp4537496p4537496.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.