How can I denominate levels of lists in R? - r

I would like to denominate the levels of a list, as with rownames() or colnames() when denominating rows and columns of matrices.
Example:
a<-matrix(rep(1,4),2,2)
b<-matrix(rep(2,9),3,3)
list<-list(a,b)
print(list)
Instead of returning [[1]] at the first level I want the list to use some string like 'matrix a' instead. Maybe this is simple to do.

Just use names:
names(list) = c("A","B")
> list
$A
[,1] [,2]
[1,] 1 1
[2,] 1 1
$B
[,1] [,2] [,3]
[1,] 2 2 2
[2,] 2 2 2
[3,] 2 2 2
list[["A"]]
[,1] [,2]
[1,] 1 1
[2,] 1 1
Note that in general it is not good practice to use R reserved words such as list as variable names.

Related

How to subset a list of a list based on the name

I am given a list of lists.
I would like now to subset the list based on the names of the sublist.
L1<-list("A"=matrix(c(1:4),2),"B"=matrix(c("a","b","c","d"),2))
L2<-list("A"=matrix(c(5:8),2),"B"=matrix(c("u","v","w","x"),2))
L<-list(L1,L2)
I would now like to select the elements of the sublist according to their name. E.g.
select_names <- c("A")
and obtain a list of lists, whereas the sublist now only contains the elements that have names belonging to 'select_names'. In this case it would be just the element "A":
[[1]]
[[1]]$`A`
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[[2]]$`A`
[,1] [,2]
[1,] 5 7
[2,] 6 8
We can Extract
library(purrr)
map(L, `[`, select_names)
#[[1]]
#[[1]]$A
# [,1] [,2]
#[1,] 1 3
#[2,] 2 4
#[[2]]
#[[2]]$A
# [,1] [,2]
#[1,] 5 7
#[2,] 6 8
Or using lapply
lapply(L, function(x) x[select_names])
Or without anonymous function call
lapply(L, `[`, select_names)
Self promotion. If one is open to using packages, I wrote up a convenience function in the developer version of manymodelr that achieves the same.
manymodelr::get_this(A,L)
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[,1] [,2]
[1,] 5 7
[2,] 6 8

Repeat rows in a data frame according to a vector

I have a data frame and would like to repeat each row by each element in a pre defined vector.
for example if I have a matrix (I use matrix for example)
matrix(c(1,2,3,2,1,3),2)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 2 3
I would like this to return
matrix(c(1,1,2,2,3,3,2,2,1,1,3,3),4)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 1 3 1
[3,] 2 2 3
[4,] 2 2 3
if the vector was vec = c(2,2).
my vector has varying size elements. Sorry, I am new to coding.
Repeat over the row numbers. In your example:
base = matrix(c(1,2,3,2,1,3),2)
rows = 1:nrow(base)
index= rep(rows, c(2,2))
base[index,]

frequency distribution of vectors

I have a list of vectors in R (permutations of {1,2,3}) like this:
> Ls
$L
[,1] [,2] [,3]
[1,] 1 2 3
$L
[,1] [,2] [,3]
[1,] 1 2 3
$L
[,1] [,2] [,3]
[1,] 2 1 3
I would like to have a frequency distribution of these vectors, i.e. the desired result should look like: 123 -> 2 and 213 -> 1.
Obviously "Table" cannot do the job. I thought about using a unique identifier for each permutation (eg: hashcoding?) but this would make the original objects unrecognizable. Someone can help?
table(do.call(paste0, do.call(rbind.data.frame, Ls)))
table can do the job~
B=unlist(lapply(LS, paste, collapse = "_"))
table(B)
B
1_2_3 2_3_1
2 1

Creating data frames from list of elements without element names [duplicate]

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Closed 7 years ago.
I would like to create a separate dataframe from list of data elements of matrices. For example, I have the following list of matrix elements:-
> A[[1]]
$`up`
,, T+1
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 1 5 7
$`down`
,, T+1
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 2 4 2
$`right`
,,T+1
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 9 2 3
Suppose that I want to create a separate list of data elements. I don't want the element names '$'up'/$'down'/$'right' to appear on my output. With the current code, every time I want to call the first matrix, I have to write the code as
A[[1]]$'up'[,,1] or A[[1]]$'down'[,,1] or A[[1]]$'right'[,,1]
Is it possible to create a separate list so that whenever I want to call it, it would be simpler without the elements name. For example, I just want to call A[[1]], whenever I want to call the first matrix, A[[2]] for the second matrix and so on. It will look something like this:-
> A[[1]]
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 1 5 7
> A[[2]]
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 2 4 2
> A[[3]]
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 9 2 3
With your definition of A you can already access the i-th matrix via A[[i]], e.g.:
> A[[1]]
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 1 5 7
Alternatively you can create a 3-dimensional array, where the third dimension indicates the number of the matrix:
B <- array(do.call("cbind", A), c(2,3,4))
Now you can access the i-th matrix (i-th z-slice of the "cube") via B[,,i], e.g.:
> B[,,2]
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 2 4 2

R indexing issue

Sorry for vague question title, i couldn't figure out something more specific.
I have 3x2 matrix c:
> c
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 2 3
It is important that ncol(c) == 2.
I also have matrix ind:
> ind
[,1] [2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 2 2
[3,] 2 2 2 1
It is important that nrow(c) == nrow(ind), and that the values of matrix ind are 1 and 2 (like column indices for each row of c)
What i want to get is matrix a with same dim as ind such that a[i,j] == c[i,ind[i,j]]:
> a
[,1] [2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 3 3
[3,] 3 3 3 2
I can do something similar in less comprehensive situations, for example if nrow(c) == 1 i'll use apply:
> apply(c,2,function(x){return(matrix(x[ind], nrow(ind)))})
I know there is a way to iterate by 2 lists using mapply, but
1) i don't know what's the best way to represent matrix as list of rows
2) i fing this solution ugly
What is the best way to achieve what i descibed here?
Matrix indexing to the rescue!
> c.mat <- matrix(c(1,1,2,2,3,3), ncol=2)
> ind <- matrix(c(2,1,2,2,1,2,2,2,2,1,2,1), ncol=4)
> matrix(c.mat[cbind(as.vector(row(ind)), as.vector(ind))], ncol=ncol(ind))
[,1] [,2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 3 3
[3,] 3 3 3 2
f<-function(x,row1){
for(i in 1:length(x)){
x[i]=cc[i,ind[i,row1]]
}
x
}
a=apply(cc,1,f,nrow(a))
You can use apply like this. Note: cc is your c matrix

Resources