Given two matrices
a <- matrix(c("a","","","d"),2,2)
b <- matrix(c("","b","",""),2,2)
a
[,1] [,2]
[1,] "a" ""
[2,] "" "d"
b
[,1] [,2]
[1,] "" ""
[2,] "b" ""
Is there an easy way to combine these two into one and get
[,1] [,2]
[1,] "a" ""
[2,] "b" "d"
without looping over each individual element?
I am interested in the problem of this "merge" in general. However, for the time being, each cell is non-empty in only one of those matrices (i.e., the case where cell [1,1] contains something in matrix a and in matrix b is ruled out).
If two matrices are of same dimension we can do :
ifelse(a == '', b, a)
# [,1] [,2]
#[1,] "a" ""
#[2,] "b" "d"
You can also do:
a[a == "" & b != ""] <- b[b != ""]
a
[,1] [,2]
[1,] "a" ""
[2,] "b" "d"
We can also use. case_when
library(dplyr)
case_when(a== '' ~ b, TRUE ~ a)
Related
This seems to be an easy task, which I am not finding a solution on R after looking up here and elsewhere. I have two matrices, one with string values and another with logical values.
a <- matrix(c(
"A", "B", "C"
))
b <- matrix(c(
T, F, T
))
> b
[,1]
[1,] TRUE
[2,] FALSE
[3,] TRUE
> a
[,1]
[1,] "A"
[2,] "B"
[3,] "C"
I need to create a third matrix that keeps values in the first that are TRUE in the second, and leaving NA on the remainder, like so:
> C
[,1]
[1,] "A"
[2,] NA
[3,] "C"
How do I achieve the above result?
C <- matrix(a[ifelse(b, T, NA)], ncol = ncol(a))
Here is an alternative by just assigning the NA to FALSE:
a[b==FALSE] <- NA
[,1]
[1,] "A"
[2,] NA
[3,] "C"
using which:
c<-a
c[which(b==FALSE)]<-NA
a <- a[b] . This might also work, Depending on how you want the result.
I want to compare two matrices. If row elements in the first matrix matches row elements in the second matrix, then I want the rows in the second matrix to be kept. If the rows do not match, then I want those rows to be to empty. I apologise that I had a quite similar question recently, but I still haven't been able to solve this one.
INPUT:
> mat1<-cbind(letters[3:8])
> mat1
[,1]
[1,] "c"
[2,] "d"
[3,] "e"
[4,] "f"
[5,] "g"
[6,] "h"
> mat2<-cbind(letters[1:5],1:5)
> mat2
[,1] [,2]
[1,] "a" "1"
[2,] "b" "2"
[3,] "c" "3"
[4,] "d" "4"
[5,] "e" "5"
Expected OUTPUT:
> mat3
[,1] [,2]
[1,] "NA" "NA"
[2,] "NA" "NA"
[3,] "c" "3"
[4,] "d" "4"
[5,] "e" "5"
I have unsuccessfully attempted this:
> mat3<-mat2[ifelse(mat2[,1] %in% mat1[,1],mat2,""),]
Error in mat2[ifelse(mat2[, 1] %in% mat1[, 1], mat2, ""), ] :
no 'dimnames' attribute for array
I have been struggling for hours, so any suggestions are welcomed.
You were on the right track, but the answer is a little simpler than what you were trying. mat2[, 1] %in% mat1[, 1] returns the matches as a logical vector, and we can just set the non-matches to NA using that vector as an index.
mat1<-cbind(letters[3:8])
mat2<-cbind(letters[1:5],1:5)
match <- mat2[,1] %in% mat1 # gives a T/F vector of matches
mat3 <- mat2
mat3[!match,] <- NA
I am a programming newbie attempting to compare two matrices. In case an element from first column in mat1 matches any element from first column in mat2, then I want that matching element in mat1 to be replaced with the neighboor (same row different column) to the match in mat2.
INPUT:
mat1<-matrix(letters[1:5])
mat2<-cbind(letters[4:8],1:5)
> mat1
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
[4,] "d"
[5,] "e"
> mat2
[,1] [,2]
[1,] "d" "1"
[2,] "e" "2"
[3,] "f" "3"
[4,] "g" "4"
[5,] "h" "5"
wished OUTPUT:
> mat3
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
[4,] "1"
[5,] "2"
I have attempted the following without succeeding:
> for(x in mat1){mat3<-ifelse(x==mat2,mat2[which(x==mat2),2],mat1)}
> mat3
[,1] [,2]
[1,] "a" "a"
[2,] "2" "b"
[3,] "c" "c"
[4,] "d" "d"
[5,] "e" "e"
Any advice will be very appreciated. Have spent a whole day without making it work. It doesn't matter to me if the elements are in a matrix or a data frame.
Thanks.
ifelse is vectorized so, we can use it on the whole column. Create the test logical condition in ifelse by checking whether the first column values of 'mat1' is %in% the first column of 'mat2', then , get the index of the corresponding values with match, extract the values of the second column with that index, or else return the first column of 'mat1'
mat3 <- matrix(ifelse(mat1[,1] %in% mat2[,1],
mat2[,2][match(mat1[,1], mat2[,1])], mat1[,1]))
mat3
# [,1]
#[1,] "a"
#[2,] "b"
#[3,] "c"
#[4,] "1"
#[5,] "2"
Here is another base R solution
v <- `names<-`(mat2[,2],mat2[,1])
mat3 <- matrix(unname(ifelse(is.na(v[mat1]),mat1,v[mat1])))
which gives
> mat3
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
[4,] "1"
[5,] "2"
An option just using logical operation rather than a function
mat3 <- mat1
mat3[mat1[,1] %in% mat2[,1], 1] <- mat2[mat2[,1] %in% mat1[,1], 2]
Subsetting the values to find those that occur in both and replacing them where they do
I got a list of matrices X rows by 2 columns, called list_of_matrices_by2
list_of_matrices_by2[1:3]
[[1]]
[,1] [,2]
[1,] "7204" "d"
[2,] "7204" "a"
[[2]]
[,1] [,2]
[1,] "2032" "b"
[2,] "2032" "e"
[3,] "2032" "a"
[[3]]
[,1] [,2]
[1,] "802" "d"
[2,] "802" "b"
I want to stack all my matrices in a all_pairs matrix, so I did this
all_pairs=do.call(rbind,list_of_matrices_by2)
all_pairs[1:10]
[,1] [,2]
[1,] "7204" "d"
[2,] "7204" "a"
[3,] "2032" "b"
[4,] "2032" "e"
[5,] "2032" "a"
[6,] "802" "d"
[7,] "802" "b"
[8,] "4674" "c"
[9,] "4674" "a"
[10,] "3886" "b"
class(all_pairs)
[1] "matrix"
For some reason, I need the rows of this matrix to be of class matrix. But it shouldn't be a problem since rows of matrix are matrix in R, right. But no!
all_pairs[1,]
[[1]]
[1] "7204"
[[2]]
[1] "d
So here are my questions :
1) Why this? How come a row of a matrix can possibly be a list?
2) What would you do to make it work, i.e. each row of my matrix has to be a matrix?
I am sure that your list_of_matrices_by2 is like:
x <- list(matrix(list("7204","7204","d","a"), ncol = 2),
matrix(list("2032","2032","2032","b","e","a"), ncol = 2),
matrix(list("802","802","d","b"), ncol = 2))
#[[1]]
# [,1] [,2]
#[1,] "7204" "d"
#[2,] "7204" "a"
#[[2]]
# [,1] [,2]
#[1,] "2032" "b"
#[2,] "2032" "e"
#[3,] "2032" "a"
#[[3]]
# [,1] [,2]
#[1,] "802" "d"
#[2,] "802" "b"
unlist(lapply(x, class))
# [1] "matrix" "matrix" "matrix"
unlist(lapply(x, mode))
# [1] "list" "list" "list"
So you do have a matrix, but it is not a matrix of list instead of numeric. You can perform rbind as usual:
y <- do.call(rbind, x)
# [,1] [,2]
#[1,] "7204" "d"
#[2,] "7204" "a"
#[3,] "2032" "b"
#[4,] "2032" "e"
#[5,] "2032" "a"
#[6,] "802" "d"
#[7,] "802" "b"
It has class "matrix", but still with mode "list". That is why when you extract the first row you get a list:
y[1, ]
#[[1]]
#[1] "7204"
#[[2]]
#[1] "d"
I don't know how you obtain those matrices. If you can control the generation process, it would be good that you end up with matrices of numeric. If you can not control it, you need convert them manually as below:
x <- lapply(x, function (u) matrix(unlist(u), ncol = 2))
unlist(lapply(x, mode))
# [1] "character" "character" "character"
Then you can do
do.call(rbind, x)
Related:
Why is this matrix not numeric?
How to create a matrix of lists?
Ok guys, I finally found a solution.
Reminder
all_pairs=do.call(rbind,list_of_matrices_by2)
Extraction of all values of my matrix into a vector
extracted_values=unlist(as.vector(t(all_pairs)))
Build the new matrix
all_pairs_new=t(matrix(data = extracted_values,nrow = 2,ncol = 10000))
I have a basic problem with R.
I have produced the matrix
M
[,1] [,2]
[1,] "a" "1"
[2,] "b" "2"
[3,] "a" "3"
[4,] "c" "1"
I would like to obtain the 3X2 matrix
[,1] [,2] [,3]
[1,] "a" "1" "3"
[2,] "b" "2" NA
[3,] "c" "1" NA
obtained by eliminating duplicates in M[,1] and writing in N[i,2], N[i,3] the values in M[,2] corresponding to the same element in M[,1], for all i's. The "NA"'s in N[,3] correspond to the singletons in M[,1].
I know how to eliminate duplicates from a vector in R: my problem is to keep track of the elements in M[,2] and write them in the resulting matrix N. I tried with for cycles but they do not work so well in my "real world" case, where the matrices are much bigger.
Any suggestions?
I thank you very much.
You can use dcast in the reshape2 package after turning your matrix to a data.frame. To reverse the process you can use melt.
df = data.frame(c("a","b","a","c"),c(1:3,1))
colnames(df) = c("factor","obs")
require(reshape2)
df2=dcast(df, factor ~ obs)
now df2 is:
factor 1 2 3
1 a 1 NA 3
2 b NA 2 NA
3 c 1 NA NA
To me it makes more sense to keep it like this. But if you need it in your format:
res = t(apply(df2,1,function(x) { newLine = as.vector(x[which(!is.na(x))],mode="any"); newLine=c(newLine,rep(NA, ncol(df2)-length(newLine) )) }))
res = res[,-ncol(res)]
[,1] [,2] [,3]
[1,] "a" " 1" " 3"
[2,] "b" " 2" NA
[3,] "c" " 1" NA