Given a Julia list of lists:
data = [[1,2],[4,5]]
which has type Vector{Int64}, how can I convert this to a 2D data type (e.g. 2×2 Matrix{Int64}) so that I can index it like data[:,2]? I tried hcat or vcat but couldn't get the result that I wanted. Thanks in advance!
You can do:
julia> reduce(hcat, data)
2×2 Matrix{Int64}:
1 4
2 5
hcat works fine:
julia> hcat([[1,2],[4,5]]...)
2×2 Matrix{Int64}:
1 4
2 5
The thing is that vectors are column-vectors in Julia (unlike in NumPy, for example), so you should horisontally concatenate them to get the matrix.
If you use vcat, you'll stack them on top of each other, getting one tall vector:
julia> vcat([[1,2],[4,5]]...)
4-element Vector{Int64}:
1
2
4
5
You can use Iterators for that. Once you have a Vector simply use reshape.
reshape( collect(Iterators.flatten([[1,2],[4,5]])), 2,2 )
2×2 Matrix{Int64}:
1 4
2 5
Related
To make, 2d array with list comprehesion, i write this:
array = [f(i) for in 1:length]
at this, function f returns 1-d list.
But at the result as nested array....
How can i create 2d array with list comprehension?
The example of 2d dimension is like this:
julia> A
2×3 Array{Float64,2}:
0.0194681 0.195811 0.150168
0.398199 0.544672 0.942663
Since your f already returns a vector (I assume you refer to this type when you write "1-d list") then it is not possible to create a matrix using a comprehension (unless you want to write f(i)[j] in the example of Przemyslaw which will be inefficient).
What you should do is:
reduce(hcat, [f(i) for i in 1:len])
to get a matrix whose columns are the values returned by f(i).
Here it is:
julia> [x*y for x in 1:5, y in 1:3]
5×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
This question already has an answer here:
More than one value for "each" argument in "rep" function?
(1 answer)
Closed 2 years ago.
Suppose I have a numeric vector v
v <- 1:5
I want to
rep
v[1] by v[1] times.
v[2] by v[2] times... and so on....
The desired output would be:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
The following does not work. Got any ideas?
rep(v, each = function(x) v[x])
Many thanks in advance.
We can use rep on itself
rep(v, v)
If we want to specify the argument, use times
rep(v, times = v)
The each would not take anonymous function and it takes only a vector of length 1. According to ?rep
each - non-negative integer. Each element of x is repeated each times. Other inputs will be coerced to an integer or double vector and the first element taken. Treated as 1 if NA or invalid.
I want to convert a list to a data frame, with the following code:
ls<-list(a=c(1:4),b=c(3:6))
do.call("rbind",ls)
The result obtained by adding do.call is as shown below. It returns a data.frame object as desired.
do.call("rbind",ls)
[,1] [,2] [,3] [,4]
a 1 2 3 4
b 3 4 5 6
However if I directly use rbind, it returns a list.
Why does rbind behave differently in these two situations?
my.df<-rbind(ls)
str(ls)
my.df
a b
ls Integer,4 Integer,4
str(ls)
List of 2
$ a: int [1:4] 1 2 3 4
$ b: int [1:4] 3 4 5 6
do.call(rbind, ls) gives you the same output as Reduce(rbind, ls). The later is less efficient, but it serves to show how you are iterating over the objects in ls rather than manipulating ls (which is a concatenated list of 2 lists) directly.
They both operate by "unlisting" each element of the list, which has class numeric. When you rbind numeric arguments, the resulting class is a matrix with typeof being integer. If you just rbind the list, each element of the list is considered a single object. So the returned object is a matrix object with 1 row and 2 columns and entries of type list. That it has 1 row should make it apparent it's treating the object ls as one thing, and not two things. Typing rbind(ls, ls, ls) will give 3 rows and 2 columns.
Is there a straightforward way to generate all possible permutations of a vector of integers (1 to max 999) that specifically excludes duplicated elements?
For example, for a vector with three elements in a range of 1 to 9 the sequence 1 2 3 would be acceptable, as would 1 2 9 but 1 2 2 would be invalid. The sequence must contain exactly n elements (in this case, three). EDIT: to avoid confusion, the order is significant, so 1 2 9 and 9 2 1 are both valid and required.
There are many questions on permutations and combinations using R on SO (such as this and this) but none that seem to fit this particular case. I'm hoping there's an obscure base R or package function out there that will take care of it without me having to write a graceless function myself.
Using gtools package:
require(gtools)
permutations(n = 9, r = 3, v = 1:9)
# n -> size of source vector
# r -> size of target vector
# v -> source vector, defaults to 1:n
# repeats.allowed = FALSE (default)
utils::combn ; combinat::combn or combinat::permn are alternatives.
EDIT: This is not what the OP asked for, but I leave this answer, to avoid confusion.
My math is a little bit rusty, but i think you are describing combinations, not permutations. The base functioncombn() returns combinations.
I illustrate with a manageable set - all combinations of length 3, from the vector 1:4:
combn(4, 3)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
The difference between combinations and permutations is that in combinations the order doesn't matter. So, (2, 3, 4) and (4, 3, 2) is the same combination, but different permutations.
When subsetting arrays, R behaves differently depending on whether one of the dimensions is of length 1 or not. If a dimension has length 1, that dimension is lost during subsetting:
ax <- array(1:24, c(2,3,4))
ay <- array(1:12, c(1,3,4))
dim(ax)
#[1] 2 3 4
dim(ay)
#[1] 1 3 4
dim(ax[,1:2,])
#[1] 2 2 4
dim(ay[,1:2,])
#[1] 2 4
From my point of view, ax and ay are the same, and performing the same subset operation on them should return an array with the same dimensions. I can see that the way that R is handling the two cases might be useful, but it's undesirable in the code that I'm writing. It means that when I pass a subsetted array to another function, the function will get an array that's missing a dimension, if I happened to reduce a dimension to length 1 at an earlier stage. (So in this case R's flexibility is making my code less flexible!)
How can I prevent R from losing a dimension of length 1 during subsetting? Is there another way of indexing? Some flag to set?
As you've found out by default R drops unnecessary dimensions. Adding drop=FALSE while indexing can prevent this:
> dim(ay[,1:2,])
[1] 2 4
> dim(ax[,1:2,])
[1] 2 2 4
> dim(ay[,1:2,,drop = F])
[1] 1 2 4