seq

Missing last sequence in seq() in R

做~自己de王妃 提交于 2019-12-01 06:43:20
问题 I have this example data by<-200 to<-seq(from=by,to=35280,by=by) Problem is that to ends at 35200 and ignore the last 80 which I need to involve in as last value. Is there any straigthforward way how to achieve it? I have tried along.with and length.out parameters but I cannot go trough. 回答1: You can place if statement for the last element of the vector such as in the following function : seqlast <- function (from, to, by) { vec <- do.call(what = seq, args = list(from, to, by)) if ( tail(vec,

Clustering rows by group based on column value

一曲冷凌霜 提交于 2019-12-01 05:20:19
问题 I have the following: df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2), Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1)) And I want this: df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2), Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1), Cluster = c(0,1,1,1,2,2,2,3,3,3,0,0,1)) How can I obtain 'Cluster' column in which I have to sequence the number of 1 until the first 0 appears, with dplyr? Consecutive 0's have to maintain the value until a new one appears. EDIT How can I do that, with many columns?

why is Seq.iter and Seq.map so much slower?

一曲冷凌霜 提交于 2019-12-01 03:13:30
Consider this code in F#: let n = 10000000 let arr = Array.init n (fun _ -> 0) let rec buildList n acc i = if i = n then acc else buildList n (0::acc) (i + 1) let lst = buildList n [] 0 let doNothing _ = () let incr x = x + 1 #time arr |> Array.iter doNothing // this takes 14ms arr |> Seq.iter doNothing // this takes 74ms lst |> List.iter doNothing // this takes 19ms lst |> Seq.iter doNothing // this takes 88ms arr |> Array.map incr // this takes 33ms arr |> Seq.map incr |> Seq.toArray // this takes 231ms! lst |> List.map incr // this takes 753ms lst |> Seq.map incr |> Seq.toList // this takes

R rep seq where number of rows is not multiple of seq length

家住魔仙堡 提交于 2019-12-01 01:32:38
I have a data frame with 1666 rows. I would like to add a column with a repeating sequence of 1:5 to use with cut() to do cross validation. It would look like this: Y x1 x2 Id1 1 .15 3.6 1 0 1.1 2.2 2 0 .05 3.3 3 0 .45 2.8 4 1 .85 3.1 5 1 1.01 2.9 1 ... ... ... ... I've tried the following 2 ways but get an error message as it seems to only add numbers in increments of the full seq() argument: > tr2$Id1 <- rep(seq(1,5,1), (nrow(tr2)/5)) Error in `$<-.data.frame`(`*tmp*`, "Id", value = c(1, 2, 3, 4, 5, 1, 2, : replacement has 1665 rows, data has 1666 > tr2$Id1 <- rep(seq(1,5,1), (nrow(tr2)/5) +

Get a seq() in R with alternating steps

孤人 提交于 2019-11-30 08:22:52
问题 The seq function in R would give me a sequence from x to y with a constant step m : seq(x, y, m) E.g. seq(1,9,2) = c(1,3,5,7,9) . What would be the most elegant way to get a sequence from x to y with alternating steps m1 and m2 , such that something like "seq(x, y, c(m1, m2))" would give me c(x, x + m1, (x + m1) + m2, (x + m1 + m2) + m1, ..., y) , each time adding one of the steps (not necessarily reaching up to y , of course, as in seq )? Example: x = 1; y = 19; m1 = 2; m2 = 4 and I get c(1

F# equivalent of LINQ Single

南楼画角 提交于 2019-11-29 16:13:15
Ok, so for most LINQ operations there is a F# equivalent. (Generally in the Seq module, since Seq= IEnumerable) I can't find the equiv of IEmumerable.Single , I prefer Single over First (which is Seq.find), because it is more defensive - it asserts for me the state is what I expect. So I see a couple of solutions (other than than using Seq.find). (These could be written as extension methods) The type signature for this function, which I'm calling only, is ('a->bool) -> seq<'a> -> 'a let only = fun predicate src -> System.Linq.Enumerable.Single<'a>(src, predicate) let only2 = Seq.filter >> Seq

Inconsistency with Clojure's sequences?

本小妞迷上赌 提交于 2019-11-29 13:20:26
Clojure: 1:13 user=> (first (conj '(1 2 3) 4)) 4 1:14 user=> (first (conj [1 2 3] 4)) 1 ; . . . 1:17 user=> (first (conj (seq [1 2 3]) 4)) 4 I understand what is going on, but should this work differently? Documentation for conj (from clojure.org ): conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the concrete type. It's more efficient to "add" elements to the end of a vector, while it's more efficient to do so at the beginning of lists. conj uses whatever is the most efficient for the data

Print a multiplication table with minimal code

风流意气都作罢 提交于 2019-11-29 12:43:59
In R, what is the fastest way(shortest code) to print multiplication table? The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this. rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3), "4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6)) Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way? tbl <- outer(1:6, 1:12, "*") rownames(tbl) <- paste(1:6, "'s", sep="") tbl You could make slightly more compact by using paste0(1:6, "'s") This seems a slight improvement: > v<

Split seq in F#

一个人想着一个人 提交于 2019-11-29 10:21:31
I should split seq<a> into seq<seq<a>> by an attribute of the elements. If this attribute equals by a given value it must be 'splitted' at that point. How can I do that in FSharp? It should be nice to pass a 'function' to it that returns a bool if must be splitted at that item or no. Sample: Input sequence: seq: {1,2,3,4,1,5,6,7,1,9} It should be splitted at every items when it equals 1, so the result should be: seq { seq{1,2,3,4} seq{1,5,6,7} seq{1,9} } All you're really doing is grouping--creating a new group each time a value is encountered. let splitBy f input = let i = ref 0 input |> Seq

Get a seq() in R with alternating steps

只愿长相守 提交于 2019-11-29 10:08:56
The seq function in R would give me a sequence from x to y with a constant step m : seq(x, y, m) E.g. seq(1,9,2) = c(1,3,5,7,9) . What would be the most elegant way to get a sequence from x to y with alternating steps m1 and m2 , such that something like "seq(x, y, c(m1, m2))" would give me c(x, x + m1, (x + m1) + m2, (x + m1 + m2) + m1, ..., y) , each time adding one of the steps (not necessarily reaching up to y , of course, as in seq )? Example: x = 1; y = 19; m1 = 2; m2 = 4 and I get c(1,3,7,9,13,15,19) . I arrived the solution by: 1. Use cumsum with a vector c(from,rep(by,times),...) ,