How to delete a specific row in Julia - julia

How can I delete a specific row in Julia? Let's say I have an array:
[A , 2
B , 4
C , 6]
I want to delete the lines for which 'B' is in the first column. I can identify which row this is, but am not able to delete this row. Can anybody help me?
Thanks,
Nico

julia> a = rand(1:10, 5,3)
5×3 Array{Int64,2}:
4 5 7
8 4 3
8 6 3
10 4 1
9 3 10
To delete row 4:
julia> row = 4
julia> a = a[setdiff(1:end, row), :]
4×3 Array{Int64,2}:
4 5 7
8 4 3
8 6 3
9 3 10

Say you have a dataframe called "data".
julia> data=DataFrame(rand(1:10, 5,3))
5×3 DataFrames.DataFrame
Row x1 x2 x3
1 9 1 1
2 8 5 8
3 9 2 2
4 9 6 5
5 3 8 7
You want to delete entire row where column x1 has value 8.
julia> data[data[:x1].!=8,:]
4×3 DataFrames.DataFrame
Row x1 x2 x3
1 9 1 1
2 9 2 2
3 9 6 5
4 3 8 7

Related

get the value of a cell of a dataframe based on the value in one of the columns in R

I have an example of a data frame in which columns "a" and "b" have certain values, and in column "c" the values are 1 or 2. I would like to create column "d" in which the value found in the frame will be located at the index specified in column "c".
x = data.frame(a = c(1:10), b = c(3:12), c = seq(1:2))
x
a b c
1 1 3 1
2 2 4 2
3 3 5 1
4 4 6 2
5 5 7 1
6 6 8 2
7 7 9 1
8 8 10 2
9 9 11 1
10 10 12 2
thus column "d" for the first row will contain the value 1, since the index in column "c" is 1, for the second row d = 4, since the index in column "c" is 2, and so on. I was not helped by the standard indexing in R, it just returns the value of the column c. in what ways can I solve my problem?
You may can create a matrix of row and column numbers to subset values from the dataframe.
x$d <- x[cbind(1:nrow(x), x$c)]
x
# a b c d
#1 1 3 1 1
#2 2 4 2 4
#3 3 5 1 3
#4 4 6 2 6
#5 5 7 1 5
#6 6 8 2 8
#7 7 9 1 7
#8 8 10 2 10
#9 9 11 1 9
#10 10 12 2 12
If the input is tibble, you need to change the tibble to dataframe to use the above answer.
If you don't want to change to dataframe, here is another option using rowwise.
library(dplyr)
x <- tibble(x)
x %>% rowwise() %>% mutate(d = c_across()[c])
By using dplyr::mutate and ifelse,
x %>% mutate(d = ifelse(c == 1, a, b))
a b c d
1 1 3 1 1
2 2 4 2 4
3 3 5 1 3
4 4 6 2 6
5 5 7 1 5
6 6 8 2 8
7 7 9 1 7
8 8 10 2 10
9 9 11 1 9
10 10 12 2 12

Julia problems with end, matrix

When I type this error jumps in julia but I don't know why, it should be working./
julia> A = [1 2 3 4; 5 6 7 8; 1 2 3 4; 5 6 7 8]
4×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
julia> B = A[2:1:end; 2:1:end]
ERROR: syntax: missing last argument in "2:1:" range expression
Stacktrace:
[1] top-level scope at REPL[9]:0
The syntax to index a multidimensional array uses a comma , instead of semicolon ; as separator between dimensions, see https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1. Thus you want to do:
julia> A = [1 2 3 4; 5 6 7 8; 1 2 3 4; 5 6 7 8]
4×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
julia> B = A[2:1:end, 2:1:end]
3×3 Array{Int64,2}:
6 7 8
2 3 4
6 7 8
Note also that you can omit 1 in the range specification, as step 1 is the default:
julia> A[2:end, 2:end]
3×3 Array{Int64,2}:
6 7 8
2 3 4
6 7 8

Add vector as row to matrix in Julia

I have a vector and a matrix (Array{T,1} and Array{T,2}) in my Julia code and I would like to append them such that the vector becomes a new row in the matrix (should be first row). I've tried several methods (cat, etc.) but keep getting errors which I believe are related to the different shape of the data. See the example below.
julia> v = Vector([1, 2, 3])
3-element Array{Int64,1}:
1
2
3
julia> m = Matrix([4 5 6; 7 8 9])
2×3 Array{Int64,2}:
4 5 6
7 8 9
julia> cat(v,m,dims=(1,2))
5×4 Array{Int64,2}:
1 0 0 0
2 0 0 0
3 0 0 0
0 4 5 6
0 7 8 9
What I actually want is
1 2 3
4 5 6
7 8 9
I realize that I can get this to work with transpose(v) but I was hoping to avoid extra calls.
Thanks!
As long as you can change the construction of v to a 1 x 3 array, you can avoid the transpose:
julia> v = [1 2 3]
1×3 Array{Int64,2}:
1 2 3
julia> m = [4 5 6; 7 8 9]
2×3 Array{Int64,2}:
4 5 6
7 8 9
julia> vcat(v, m)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
I think that just doing the transpose
julia> v2 = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> vcat(v2', m)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
is almost as efficient though.

Create set of matrices from concatenating columns of another matrix in r

I have two matrices A and B of dimension 5 by 3 and 5 by 2, respectively. I want to produce series of matrices combining each column of matrix B to A. The dimensions of the resulting matrices would be 5 by 4
Let A be
1 2 3
4 5 6
7 8 9
2 3 1
4 1 5
and B be
1 2
2 5
3 8
6 3
2 1
Then the resulting matrices are
1 2 3 1
4 5 6 2
7 8 9 3
2 3 1 6
4 1 5 2
and
1 2 3 2
4 5 6 5
7 8 9 8
2 3 1 3
4 1 5 1
Use our old friend the assignment operator. Assigning 1st column of B to 4th of A:
A[, 4] <- B[, 1]
> A
V1 V2 V3 V4
1 1 2 3 1
2 4 5 6 2
3 7 8 9 3
4 2 3 1 6
5 4 1 5 2
Then A[, 4] <- B[, 2], etc.

Combine minimum values of row and column in matrix

Suppose I have a vector of size n=8 v=(5,8,2,7,9,12,2,1). I would like to know how to build a N x N matrix that compares every pair of values of v and returns the minimum value of each comparation. In this example, it would be like this:
5 5 2 5 5 5 2 1
5 8 2 7 8 8 2 1
2 2 2 2 2 2 2 1
5 7 2 7 7 7 2 1
5 8 2 7 9 9 2 1
5 8 2 7 9 12 2 1
2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1
Could you help me with this, please?
outer(v, v, pmin)
Notice the use of pmin, not min, as the former is vectorised but not the latter.

Resources