[R] Order sapply
Sarah Goslee
sarah.goslee at gmail.com
Fri Apr 6 19:21:51 CEST 2012
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
More information about the R-help
mailing list