I have two matrices I want to sum based on their row and column names. The matrices will not necessarily have all rows and columns in common - some may be missing from either matrix.
For example, consider two matrices A and B:
A= B=
a b c d a c d e
v 1 1 1 0 v 0 0 0 1
w 1 1 0 1 w 0 0 1 0
x 1 0 1 1 y 0 1 0 0
y 0 1 1 1 z 1 0 0 0
Column e is missing from matrix A and column b is missing from matrix B.
Row z is missing from matrix A and row x is missing from matrix B.
The summed table I'm looking for is:
Sum=
a b c d e
v 1 1 1 0 1
w 1 1 0 2 0
x 1 0 1 1 na
y 0 1 2 1 0
z 1 na 0 0 0
The row and column ordering in the final matrix don't matter, as long as the matrix is complete, i.e. has all the data. Missing values don't have to be "Na", but could be "0" instead.
I'm not sure if there is a way to do this that doesn't involve for loops. Any help would be much appreciated.
My solution
I managed to do this easily by converting the matrices to dataframes, binding the dataframes by row and then casting the resulting dataframe back into a matrix. This looks like it works, but maybe someone could double check or let me know if there is a better way.
library(reshape2)
A_df=as.data.frame(as.table(A))
B_df=as.data.frame(as.table(B))
merged_df=rbind(A_df,B_df)
Summed_matrix=acast(merged_df, Var1 ~ Var2, sum)
merged_df looks like this:
Var1 Var2 Freq
1 v a 1
2 w a 1
3 x a 1
4 y a 0
5 v b 1
6 w b 1
etc...
May be you can try:
cAB <- union(colnames(A), colnames(B))
rAB <- union(rownames(A), rownames(B))
A1 <- matrix(0, ncol=length(cAB), nrow=length(rAB), dimnames=list(rAB, cAB))
B1 <- A1
indxA <- outer(rAB, cAB, FUN=paste) %in% outer(rownames(A), colnames(A), FUN=paste)
indxB <- outer(rAB, cAB, FUN=paste) %in% outer(rownames(B), colnames(B), FUN=paste)
A1[indxA] <- A
B1[indxB] <- B
A1+B1 #because it was mentioned to have `0` as missing values
# a b c d e
#v 1 1 1 0 1
#w 1 1 0 2 0
#x 1 0 1 1 0
#y 0 1 2 1 0
#z 1 0 0 0 0
If you want to get the NA as missing values
A1 <- matrix(NA, ncol=length(cAB), nrow=length(rAB), dimnames=list(rAB, cAB))
B1 <- A1
A1[indxA] <- A
B1[indxB] <- B
indxNA <- is.na(A1) & is.na(B1)
A1[is.na(A1)!= indxNA] <- 0
B1[is.na(B1)!= indxNA] <- 0
A1+B1
# a b c d e
#v 1 1 1 0 1
#w 1 1 0 2 0
#x 1 0 1 1 NA
#y 0 1 2 1 0
#z 1 NA 0 0 0
Or using reshape2
library(reshape2)
acast(rbind(melt(A), melt(B)), Var1~Var2, sum) #Inspired from the OP's idea
# a b c d e
#v 1 1 1 0 1
#w 1 1 0 2 0
#x 1 0 1 1 0
#y 0 1 2 1 0
#z 1 0 0 0 0
data
A <- structure(c(1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L,
1L, 1L, 1L), .Dim = c(4L, 4L), .Dimnames = list(c("v", "w", "x",
"y"), c("a", "b", "c", "d")))
B <- structure(c(0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L,
0L, 0L, 0L), .Dim = c(4L, 4L), .Dimnames = list(c("v", "w", "y",
"z"), c("a", "c", "d", "e")))
Related
I want to replace multiple columns of a data frame by one column each for each group whereas I also want to change the numbers. Example:
A1 A2 A3 A4 B1 B2 B3
1 1 1 0 1 1 0 0
2 1 0 1 1 0 1 1
3 1 1 1 1 0 1 1
4 0 0 1 0 0 0 1
5 0 0 0 0 0 1 0
I want to sort this data frame by it's headers meaning I only want one column "A" instead of 4 here and only column "B" instead of 3 here. The numbers should change with the following pattern: If you are in group "A2" and the observation has the number "1" it should be changed into a "2" instead. If you are in group "A3" and the observation has the number "1" it should be changed into a "3" instead. The end result should be that I want to contain the highest number in that specific column and row (if I have 3 "1"s in my row and group, the number which is going to replace all of them is going to be the one of the highest group)
If the number is 0 then nothing changes. Here is the result I'm looking for:
A B
1 4 1
2 4 3
3 4 3
4 3 3
5 0 2
How can I replace all of these groups by a single column each? (one column for each group)
So far I've tried a lot with the function unite(data= testdata, col= "A") for example, but doing this manually would take too long. There has to be a better way, right?
Thanks in advance!
You can do:
dat <- read.table(header=TRUE, text=
"A1 A2 A3 A4 B1 B2 B3
1 1 1 0 1 1 0 0
2 1 0 1 1 0 1 1
3 1 1 1 1 0 1 1
4 0 0 1 0 0 0 1
5 0 0 0 0 0 1 0")
myfu <- function(x) if (any(x)) max(which(x)) else 0
new <- data.frame(
A=apply(dat[, 1:4]==1, 1, myfu),
B=apply(dat[, 5:7]==1, 1, myfu))
new
A more general solution:
new2 <- data.frame(
A=apply(dat[, grepl("^A", names(dat))]==1, 1, myfu),
B=apply(dat[, grepl("^B", names(dat))]==1, 1, myfu))
new2
You can try the code like below
dfout <- as.data.frame(
lapply(
split.default(df, gsub("\\d+$", "", names(df))),
function(v) max.col(v, ties.method = "last") * +(rowSums(v) >= 1)
)
)
such that
> dfout
A B
1 4 1
2 4 3
3 4 3
4 3 3
5 0 2
Data
df <- structure(list(A1 = c(1L, 1L, 1L, 0L, 0L), A2 = c(1L, 0L, 1L,
0L, 0L), A3 = c(0L, 1L, 1L, 1L, 0L), A4 = c(1L, 1L, 1L, 0L, 0L
), B1 = c(1L, 0L, 0L, 0L, 0L), B2 = c(0L, 1L, 1L, 0L, 1L), B3 = c(0L,
1L, 1L, 1L, 0L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5"))
assuming your data is in a data.frame called df1 this works in Base-R
df1 <- t(df1)*as.numeric(regmatches(colnames(df1), regexpr("\\d+$", colnames(df1))))
df1 <- split(as.data.frame(df1),sub("\\d+$","",row.names(df1)))
df1 <- sapply(df1, apply, 2, max)
output:
> df1
A B
1 4 1
2 4 3
3 4 3
4 3 3
5 0 2
I have a data set like this:
A B C D E F G
12 1 0 0 0 0 0
Hey 0 1 0 0 0 0
No 0 0 0 0 0 1
Yes 0 0 0 0 1 0
I want to build an scenario, what will happen if a COLUMN has 10% more YES (yes = 1). But, in my scenario, this should be done with 3 columns at the same time.
So: let's say that the rows of interest are where B=1 or C=1 or D=1. If one of the columns is equal to 1, that is fine. But I want to (randomly) make 10% of the remaining rows (where B=0 & C=0 & D=0) into a 1 (and of course, if we give them (the randomly 10% of the remaining rows) a 1, then the other columns should be all 0 (except column A)).
Sorry, really had a hard time to explain this problem. Hopefully it is clear.
The result should be something like this (it is not representing the 10% since the example is too small).
A B C D E F G
12 1 0 0 0 0 0
Hey 0 1 0 0 0 0
No 0 0 0 0 0 1
Yes 0 1 0 0 0 0
where you can see that "Yes" is randomly assigned as C=1, and its original value is set back to 0.
I believe this is what you want:
data:
df1<-
structure(list(A = c("12", "Hey", "No", "Yes"), B = c(1L, 0L,
0L, 0L), C = c(0L, 1L, 0L, 0L), D = c(0L, 0L, 0L, 0L), E = c(0L,
0L, 0L, 0L), F = c(0L, 0L, 0L, 1L), G = c(0L, 0L, 1L, 0L)), row.names = c(NA,
-4L), class = "data.frame")
code:
m <- `rownames<-`(df1[,-1],df1[,1]) # make your life simple, add character col as rownames
percentage = .5 # choose any percentage you like from 0 to 1, .1 for 10%
amountOf1 = floor(percentage * ncol(m)) # get the amount of ones based on percentage
IND <- which(rowSums(m[,1:3]) == 0) # get those rows having B, C, D with 0
for(i in IND) {
m[i,] = sample(rep(1:0,c(amountOf1,ncol(m)-amountOf1)) )
}
result: (now 50% are 1 in rows where B,C,D is 0)
# B C D E F G
#12 1 0 0 0 0 0
#Hey 0 1 0 0 0 0
#No 1 0 0 0 1 1
#Yes 1 0 1 0 0 1
I have a dataframe in R which looks like the one below.
a b c d e f
0 1 1 0 0 0
1 1 1 1 0 1
0 0 0 1 0 1
1 0 0 1 0 1
1 1 1 0 0 0
The database is big, spanning over 100 columns and 5000 rows and contain all binaries (0's and 1's). I want to construct an overlap between each and every columns in R. Something like the one given below. This overlap dataframe will be a square matrix with equal number of rows and columns and that will be same as the number of columns in the 1st dataframe.
a b c d e f
a 3 2 2 2 0 2
b 2 3 3 3 0 1
c 2 3 3 1 0 1
d 2 3 1 3 0 3
e 0 0 0 0 0 0
f 2 1 1 3 0 3
Each cell of the second dataframe is populated by the number of cases where both row and column have 1 in the first dataframe.
I'm thinking of constructing a empty matrix like this:
df <- matrix(ncol = ncol(data), nrow = ncol(data))
colnames(df) <- names(data)
rownames(df) <- names(data)
.. and iterating over each cell of this matrix using an apply command reading the corresponding row name (say, x) and column name (say, y) and running a function like the one below.
summation <- function (x,y) (return (sum(data$x * data$y)))
The problem with is I can't find out the row name and column name while within an apply function. Any help will be appreciated.
Any more efficient way than what I'm thinking is more than welcome.
You are looking for crossprod
crossprod(as.matrix(df1))
# a b c d e f
#a 3 2 2 2 0 2
#b 2 3 3 1 0 1
#c 2 3 3 1 0 1
#d 2 1 1 3 0 3
#e 0 0 0 0 0 0
#f 2 1 1 3 0 3
data
df1 <- structure(list(a = c(0L, 1L, 0L, 1L, 1L), b = c(1L, 1L, 0L, 0L,
1L), c = c(1L, 1L, 0L, 0L, 1L), d = c(0L, 1L, 1L, 1L, 0L), e = c(0L,
0L, 0L, 0L, 0L), f = c(0L, 1L, 1L, 1L, 0L)), .Names = c("a",
"b", "c", "d", "e", "f"), class = "data.frame", row.names = c(NA,
-5L))
I have a dataset(nm) as shown below:
nm
2_V2O 10_Kutti 14_DD 15_TT 16_DD 19_V2O 20_Kutti
0 1 1 0 0 1 0
1 1 1 1 1 0 0
0 1 0 1 0 0 1
0 1 1 0 1 0 0
Now I want to have multiple new datasets which got segregated as per their unique column names. All dataset names also must be created as per their column names as shown below:
Kutti
10_Kutti 20_Kutti
1 0
1 0
1 1
1 0
V2O
2_V2O 19_V2O
0 1
1 0
0 0
0 0
DD
14_DD 16_DD
1 0
1 1
0 0
1 1
TT
16_TT
0
1
0
1
I know this can be done using "select" function in dplyr but I need one dynamic programme which builds this automatically for any dataset.
We can split by the substring of the column names of 'nm'. Remove the prefix of the columnames until the _ with sub and use that to split the 'nm'.
lst <- split.default(nm, sub(".*_", "", names(nm)))
lst
#$DD
# 14_DD 16_DD
#1 1 0
#2 1 1
#3 0 0
#4 1 1
#$Kutti
# 10_Kutti 20_Kutti
#1 1 0
#2 1 0
#3 1 1
#4 1 0
#$TT
# 15_TT
#1 0
#2 1
#3 1
#4 0
#$V2O
# 2_V2O 19_V2O
#1 0 1
#2 1 0
#3 0 0
#4 0 0
It is better to keep the data.frames in a list. If we insist that it should be individual data.frame objects in the global environment (not recommended), use list2env
list2env(lst, envir = .GlobalEnv)
Now, just call
DD
data
nm <- structure(list(`2_V2O` = c(0L, 1L, 0L, 0L), `10_Kutti` = c(1L,
1L, 1L, 1L), `14_DD` = c(1L, 1L, 0L, 1L), `15_TT` = c(0L, 1L,
1L, 0L), `16_DD` = c(0L, 1L, 0L, 1L), `19_V2O` = c(1L, 0L, 0L,
0L), `20_Kutti` = c(0L, 0L, 1L, 0L)), .Names = c("2_V2O", "10_Kutti",
"14_DD", "15_TT", "16_DD", "19_V2O", "20_Kutti"), class = "data.frame",
row.names = c(NA, -4L))
I have a matrix with many rows and columns (rxp), I am trying to create a sub matrices that contains only those rows and columns that have identical cell values. For example
This is my matrix
a b c d
a 0 1 1 1
b 1 0 0 1
c 1 0 0 1
d 0 1 0 0
e 0 1 1 1
Here row b, c have identical values so the code should create, 1st sub matrix with only b and c rows and 2nd sub matrix with rows a and e
a b c d
b 1 0 0 1
c 1 0 0 1
a b c d
a 0 1 1 1
e 0 1 1 1
Presumably there can be more than one set of repeated rows so if m is your matrix this creates a list of matrices in which each such matrix has rows that are repeated:
DF <- as.data.frame(m)
Filter(function(x) nrow(x) > 1, split(DF, do.call(paste, DF)))
You could use duplicated in both directions.
m[duplicated(m) | duplicated(m, fromLast=TRUE),]
# a b c d
# b 1 0 0 1
# c 1 0 0 1
Where m is
structure(c(0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L,
1L, 1L, 0L), .Dim = c(4L, 4L), .Dimnames = list(c("a", "b", "c",
"d"), c("a", "b", "c", "d")))
You could also use.
indx <- which(duplicated(m)) #m from #Richard Scriven post
returns a list of matrices
lapply(indx, function(i) m[colSums(t(m)==m[i,])==ncol(m),])
[[1]]
# a b c d
#b 1 0 0 1
#c 1 0 0 1
[[2]]
# a b c d
#a 0 1 1 1
#e 0 1 1 1