Repeat rows of a data.frame [duplicate]
df <- data.frame(a = 1:2, b = letters[1:2]) df[rep(seq_len(nrow(df)), each = 2), ]
df <- data.frame(a = 1:2, b = letters[1:2]) df[rep(seq_len(nrow(df)), each = 2), ]
I’m a fan of the KRON function: >> a = 1:3; >> N = 3; >> b = kron(a,ones(1,N)) b = 1 1 1 2 2 2 3 3 3 You can also look at this related question (which dealt with replicating elements of 2-D matrices) to see some of the other solutions involving matrix … Read more
In ES6 using Array fill() method console.log( Array(5).fill(2) ) //=> [2, 2, 2, 2, 2]
Problem Statement We have an array of values, vals and runlengths, runlens: vals = [1,3,2,5] runlens = [2,2,1,3] We are needed to repeat each element in vals times each corresponding element in runlens. Thus, the final output would be: output = [1,1,3,3,2,5,5,5] Prospective Approach One of the fastest tools with MATLAB is cumsum and is … Read more
When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case with Javascript. That is, in Javascript, if you have a pattern with N capturing groups, you can only … Read more
These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write: “a”.repeat(10) Before repeat, we used this hack: Array(11).join(“a”) // create string with 10 a’s: “aaaaaaaaaa” (Note that an array of length 11 gets you only 10 “a”s, … Read more
To my knowledge they are identical. I think there maybe a few engines out there that don’t support the numbered syntax but I’m not sure which. I vaguely recall a question on SO a few days ago where explicit notation wouldn’t work in Notepad++. The only time I would use explicitly numbered repetition is when … Read more