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 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.0000000 3 126 126 1 197 197 197.0000000 3 35 56 1 31 31 31.0000000 ?. 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.
On Fri, Apr 6, 2012 at 10:27 AM, MSousa <ricardosousa2000 at clix.pt> 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.0000000 > 3 ? ? ? 126 ? ? 126 ? ? 1 ? ? ? 197 ? ? 197 ? ? 197.0000000 > ?3 ? ? ? ? ? ? ?35 ? ? ? ? ? ? 56 ? ? 1 ? ? ? ? 31 ? ? ?31 ? ? ?31.0000000 > ?. > > > 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 at 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.
On Fri, Apr 6, 2012 at 10:27 AM, MSousa <ricardosousa2000 at clix.pt> 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.0000000 > 3 ? ? ? 126 ? ? 126 ? ? 1 ? ? ? 197 ? ? 197 ? ? 197.0000000 > ?3 ? ? ? ? ? ? ?35 ? ? ? ? ? ? 56 ? ? 1 ? ? ? ? 31 ? ? ?31 ? ? ?31.0000000 > ?. > > > 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
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.
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.