Julia dataeye not working - julia

I was following allow with the YouTube Video on "Data Analysis in Julia with Data Frames (John Myles White)"
I got to the Convenience constructors section, dataeye, etc... however, I get the following in Julia 0.3.3:
julia> dm = dataeye(2)
ERROR: dataeye not defined
Where is the dataeye() function located/defined?
BTW: where is the RDatasets, that is used in the video?

I think dataeye used to be in DataArrays (and hence DataFrames), it doesn't seem to be used anymore and I can't find it on github for either of these projects. It looks like it was a shorthand/is not written:
#data eye(2)
julia> #data eye(4)
4x4 DataArray{Float64,2}:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
All I could find was this discussion saying they're not very useful (presumably they were then removed)...

Related

In Julia, after A = B, changing elements of B will modify A as well. Is it so? [duplicate]

This question already has answers here:
Creating copies in Julia with = operator
(2 answers)
Closed last year.
While reading Julia documentation in the section "Noteworthy differences from C/C++":
"Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well. Updating operators like += do not operate in-place, they are equivalent to A = A + B which rebinds the left-hand side to the result of the right-hand side expression."
But I tried this, it is not happening i.e. changing B is not changing A. (I tried with 2x2 Matrix)
Can someone explain what this para means?
i tried the following code:
julia> m=Array(zeros(2,2))
2×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
julia> k=m
2×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
julia> k
2×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
julia> m=Array(ones(2,2))
2×2 Matrix{Float64}:
1.0 1.0
1.0 1.0
julia> k
2×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
As it is explained
changing elements of B will modify A as well
So you can use the dotted version of assignment to change elements:
julia> m .= Array(ones(2,2))

Lag and lead in Julia

I want to turn an array like this
[1,2,3,4,5]
into a lagged version
[missing,1,2,3,4] # lag 1
[missing,missing,1,2,3] # lag 2
or a led version
[2,3,4,5,missing] # lead 1
[3,4,5,missing,missing] # lead 2
As Julia is designed for scientific computing, there must be something like this, right?
Add ShiftedArrays. See: https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162
Quoting from the above:
lag, lead functions, to shift an array and add missing (or a custom default value in the latest not yet released version) where the data is not available, or circshift for shifting circularly in a lazy (non allocating) way:
julia> v = [1.2, 2.3, 3.4]
3-element Array{Float64,1}:
1.2
2.3
3.4
julia> lag(v)
3-element ShiftedArrays.ShiftedArray{Float64,Missings.Missing,1,Array{Float64,1}}:
missing
1.2
2.3
Note the ShiftedArray version of lag keeps the array size the same. You might add a short function to make it behave the way you asked:
biglag(v, n) = lag(vcat(v, v[1:n]), n)

Large Matrix Powers in Julia

I am working with very large graphs and their corresponding weighted adjacency matrices, and I need to take these large matrices to similarly large powers (i.e. raising matrices to the power of tens of thousands).
The issue I have run into is that elements of the matrix quickly become too large for the computer to handle, and I am wondering how to get around this problem.
Has anyone worked with such problems before(raising matrices to large powers), and how did you resolve them?
I know Python's numpy can handle these computations. Is there an analogous library in Julia that can do this as well?
You could do a transfomation of type to BigFloat:
julia> A = [1.5 2 -4; 3 -1 -6; -10 2.3 4]
3×3 Array{Float64,2}:
1.5 2.0 -4.0
3.0 -1.0 -6.0
-10.0 2.3 4.0
julia> (BigFloat.(A))^32000
3×3 Array{BigFloat,2}:
4.16164e+31019 8.71351e+31017 -3.22788e+31019
4.60207e+31019 9.63565e+31017 -3.56949e+31019
-5.83403e+31019 -1.22151e+31018 4.52503e+31019

Access data deep in a structure using get()

I have a p structure in R memory and I'm trying to access the Rate column of the matrix.
When I type p$6597858$Sample in the console, I get ...
p$`6597858`$Sample
Rate Available X Y
[1,] 1.01 1520.93 0.00 0.0
[2,] 1.02 269.13 0.00 0.0
[3,] 1.03 153.19 0.00 0.0
[4,] 1.04 408.80 0.00 0.0
and so on ...
Within my code when I try to
get("p$`6597858`$Sample[,1]")
I get this returned ...
object 'p$`6597858`$Sample[ ,1]' not found
Is this an apostrophe problem?
Neither the $ nor the [[ operator work within get() (because p[[1]] is not an R object, it's a component of the object p).
You could try
p <- list(`6597858`=list(Sample=data.frame(Rate=1:3,Available=2:4)))
z <- eval(parse(text="p$`6597858`$Sample[,1]"))
but it's probably a bad idea. Is there a reason that
z <- p[["6597858"]][["Sample"]][,"Rate"]
doesn't do what you want?
You can do this dynamically by using character variables to index, still without using get: for example
needed <- 1234
x <- p[[as.character(needed)]][["Sample"]][,"Rate"]
(edit: suggested by Hadley Wickham in comments) or
x <- p[[c(as.character(needed),"Sample","Rate")]]
(if the second-lowest-level element is a data frame or list: if it's a matrix, this alternative won't work, you would need p[[c(as.character(needed),"Sample")]][,"Rate"] instead)
This is a situation where figuring out the idiom of the language and working with it (rather than struggling against it) will pay off ...
library(fortunes)
fortune(106)
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
In general,
extracting elements directly from lists is better (safer, leads to less convoluted code) than using get()
using non-standard names for list elements (i.e. those such as pure numbers that need to be protected by backticks) is unwise
[[ is more robust than $

What is the difference between sort() and sort.list() in R?

Well, having decided to get to know some of the basic functions in R I've stumbled upon the sort.list() function. I get the pretty straight forward sort() function, but don't get the idea of the sort.list(). I've read that it should be a permutation function rearranging the content of my vector (in some way).
Having the vector;
x <- c(5.0, 3.0, 2.0, 2.2, 0.0, 5.0, 3.0, 2.0, 2.2)
Running sort.list(x) outputs
[1] 5 3 8 4 9 2 7 1 6
Where did that come from? Can someone give me a hint please? And what's the use of this permutation anyway?
Thanks.
sort.list, as it says at ?sort.list, is the same as order, only instead of accepting multiple arguments via ..., it accepts only one atomic vector as an argument.
Presumably, then, it could be intended as a "faster" or "simpler" version of order.
What good is it? Consider this:
x <- c(5.0, 3.0, 2.0, 2.2, 0.0, 5.0, 3.0, 2.0, 2.2)
> x[sort.list(x)]
[1] 0.0 2.0 2.0 2.2 2.2 3.0 3.0 5.0 5.0
> x[order(x)]
[1] 0.0 2.0 2.0 2.2 2.2 3.0 3.0 5.0 5.0
Just like order it returns a permutation that when used to index the original vector sorts it.
But I also think the name is confusing.

Resources