I have a list of matrices and a list of vectors, and I want to divide the columns of each matrix with the corresponding vector element.
For example, given
set.seed(230)
data <- list(cbind(c(NA, rnorm(6)),c(rnorm(6),NA)), cbind(runif(7), runif(7)))
divisors <- list(c(0.5,2), c(3,4))
I'm looking for a vectorized function that produces output that looks the same as
for(i in 1:length(data)){
for(j in 1:ncol(data[[i]])){data[[i]][,j] <- data[[i]][,j] / divisors[[i]][j]}
}
i.e.
[[1]]
[,1] [,2]
[1,] NA 0.28265752
[2,] -0.46967014 -0.07132588
[3,] 0.20253439 -0.37432527
[4,] 0.65736410 0.06630705
[5,] 0.72349294 0.67202129
[6,] 0.88532648 -0.80892508
[7,] 0.08162027 NA
[[2]]
[,1] [,2]
[1,] 0.26597435 0.18120979
[2,] 0.31213250 0.16493883
[3,] 0.19250804 0.14104145
[4,] 0.21196882 0.10172964
[5,] 0.10389773 0.04979742
[6,] 0.02754329 0.15064043
[7,] 0.25771766 0.23042586
The closest I have been able to come is
Map(`/`, data, divisors)
But that divides rows (rather than columns) of the matrix by the vector. Any help appreciated.
Transpose your matrices before and after:
lapply(Map(`/`, lapply(data, t), divisors), t)
# [[1]]
# [,1] [,2]
# [1,] NA 0.28265752
# [2,] -0.46967014 -0.07132588
# [3,] 0.20253439 -0.37432527
# [4,] 0.65736410 0.06630705
# [5,] 0.72349294 0.67202129
# [6,] 0.88532648 -0.80892508
# [7,] 0.08162027 NA
#
# [[2]]
# [,1] [,2]
# [1,] 0.26597435 0.18120979
# [2,] 0.31213250 0.16493883
# [3,] 0.19250804 0.14104145
# [4,] 0.21196882 0.10172964
# [5,] 0.10389773 0.04979742
# [6,] 0.02754329 0.15064043
# [7,] 0.25771766 0.23042586
I prefer the transpose approach above, but another option is to expand your divisor vectors into matrices of the same dimensions as in data:
div_mat = Map(matrix, data = divisors, nrow = sapply(data, nrow), ncol = 2, byrow = T)
Map("/", data, div_mat)
Related
I'm trying to subset number of rows in a list using R.
I have 2 lists one has matrix with n rows and p columns the second list has the number of rows that I need to subset.
mat <- list(a = matrix(rnorm(8*4),8), b = matrix(rnorm(15*4),15), c = matrix(rnorm(7*4),7))
rw <- list(a = 6, b = 7, c = 4)
Both list have common names, in the above example, I would like to retain for element a first 6 rows, for b first 7 rows and c 4 rows.
How would you do that in R
One solution with Map:
Map(function(x, y) x[1:y, ], mat, rw)
# $a
# [,1] [,2] [,3] [,4]
# [1,] 1.3331549 -0.6985623 -1.1842788 -0.1496880
# [2,] 0.2096395 -0.2901906 0.4210395 0.9116542
# [3,] 0.1763317 1.3858205 -1.1567526 -1.1794618
# [4,] 1.3596395 0.5815012 -0.3681799 -0.6569447
# [5,] 0.2251352 0.2331387 -1.2509844 -1.1346729
# [6,] 0.6796729 1.1274772 0.3992489 0.2305927
#
# $b
# [,1] [,2] [,3] [,4]
# [1,] 0.30700748 -1.2173855 -0.3377885 -0.6748974
# [2,] 1.09506443 -0.6142685 -1.1301122 -0.7792081
# [3,] -0.61049306 -1.3414474 0.9771373 1.0191636
# [4,] 0.66687294 -0.5269721 0.9971987 -0.6514121
# [5,] 0.54623236 0.9020964 0.3252700 -0.3925129
# [6,] -0.04848903 -0.5204047 0.3344675 -0.3232105
# [7,] -0.56502719 -0.3743275 2.1760364 -0.2941956
#
# $c
# [,1] [,2] [,3] [,4]
# [1,] -0.3225609 -0.40126955 -1.787255 -1.5005721
# [2,] 0.3474430 -1.16657015 1.106033 0.3114282
# [3,] 0.4099467 -0.04353555 0.838330 0.3282246
# [4,] -1.4648740 0.51279791 0.198768 -0.3394502
Let's say I have this list of matrices:
c1 <- matrix(rnorm(10),5,2)
c2 <- c1+(rnorm(10))
c3 <- c1+(rnorm(10))
c4 <- c1+(rnorm(10))
c5 <- c1+(rnorm(10))
c6 <- c1+(rnorm(10))
clist <- list(c1,c2,c3,c4,c5,c6)
[[1]]
[,1] [,2]
[1,] -0.1591251 0.36887661
[2,] 0.4200732 -1.21884880
[3,] -0.6763903 -0.02593779
[4,] 0.1658612 -0.65441390
[5,] -1.4652644 -0.10981210
[[2]]
[,1] [,2]
[1,] -1.5475582 1.33232706
[2,] 0.9781123 -0.70260202
[3,] -1.1577471 2.04805617
[4,] 0.4535016 -1.08563438
[5,] -3.0072380 0.06337565
[[3]]
[,1] [,2]
[1,] 0.5475332 -0.7793278
[2,] -1.8806731 -1.1158255
[3,] -0.4837955 -0.8165737
[4,] -1.4951387 -0.2655842
[5,] -0.1487497 -0.4243752
[[4]]
[,1] [,2]
[1,] -1.270525331 0.5796936
[2,] 1.309900315 -2.4646281
[3,] -2.313890536 1.5281795
[4,] 0.003287924 -2.3560008
[5,] -1.903412482 -2.6763855
[[5]]
[,1] [,2]
[1,] -0.4553650 0.06665067
[2,] -0.4382334 -0.91694728
[3,] -1.8101902 0.29204456
[4,] 0.6602221 -0.45068171
[5,] -1.3796827 0.51264234
[[6]]
[,1] [,2]
[1,] -1.0130324 1.4233890
[2,] 0.9672156 -0.9425755
[3,] -2.5090911 -0.5489537
[4,] 0.7705731 1.0351301
[5,] -0.0414573 -1.8325651
I want to merge c1+c2, c3+c4, c5+c6 and keep them in a list. I could this manually with the following code:
cm1 <- do.call("rbind", clist[1:2])
cm2 <- do.call("rbind", clist[3:4])
cm3 <- do.call("rbind", clist[5:6])
cmlist <- list(cm1, cm2, cm3)
But because my actual data will be much larger, this method would be very time consuming. Is there a much quicker way to do it?
Try this:
cmlist1=lapply(seq(1,length(clist),by=2),function(x)do.call("rbind", clist[x:(x+1)]))
How about the following?
mapply(rbind,
clist[which(seq(1,length(clist)) %% 2 == 1)],
clist[which(seq(1,length(clist)) %% 2 == 0)],
SIMPLIFY = F)
We create a grouping variable g (equal to c(1, 1, 2, 2, ...)) and then split by it and rbind the elements of each component together:
n <- length(clist)
g <- c(gl(n, 2, n))
lapply(split(clist, g), "do.call", what = "rbind")
I have a matrix with 2 columns, and I'd like to turn it into a matrix with specified dimensions.
> t <- matrix(rnorm(20), ncol=2, nrow=10)
[,1] [,2]
[1,] 1.4938530 1.2493088
[2,] -0.8079445 1.8715868
[3,] 0.5775695 -0.9277420
[4,] 0.4415969 2.6357908
[5,] 0.3209226 -1.1306049
[6,] 0.5109251 -0.8661100
[7,] 1.9495571 0.2092941
[8,] 0.7816373 1.1517466
[9,] 0.0300595 -0.1351532
[10,] 0.7550894 0.7778869
What I'd like to do is something like:
> tt <- matrix(t, ncol=4, nrow=5)
[,1] [,2] [3,] [4,]
[1,] 1.4938530 1.2493088 -0.8079445 1.8715868
[2,] 0.5775695 -0.9277420 0.4415969 2.6357908
[3,] etc.
I tried to do things with modulo but my head hurts too much for me to try even one more minute.
You can transpose your first matrix, so that data is stored in the order you want, and then fill the second matrix by row:
tt <- matrix(t(t), ncol=4, nrow=5, byrow = T)
t
# [,1] [,2]
# [1,] -1.4162465950 0.01532476
# [2,] -0.2366332875 -0.04024386
# [3,] 0.5146631983 -0.34720239
# [4,] 1.9243922633 -0.24016160
# [5,] 1.6161165230 0.63187438
# [6,] -0.3558181508 -0.73199138
# [7,] 0.7459405376 0.01934826
# [8,] -1.0428581093 -2.04422042
# [9,] 0.0003166344 0.98973993
#[10,] 0.6390745275 -0.65584930
tt
# [,1] [,2] [,3] [,4]
# [1,] -1.4162465950 0.01532476 -0.2366333 -0.04024386
# [2,] 0.5146631983 -0.34720239 1.9243923 -0.24016160
# [3,] 1.6161165230 0.63187438 -0.3558182 -0.73199138
# [4,] 0.7459405376 0.01934826 -1.0428581 -2.04422042
# [5,] 0.0003166344 0.98973993 0.6390745 -0.65584930
When you work with matrix in R, you can think of it as a vector with data stored column by column. So extracting data by row from a matrix is not as straight forward as extracting by column which is essentially how data is stored. After transposing the first matrix, the data will be stored in an order you want to extract and then fill the second matrix by row would be straight forward.
I wonder how to compute pairwise Lepage statistic between columns on data like:
> cbind(v1=rnorm(10), v2=rnorm(10), v3=rnorm(10), v4=rnorm(10))
v1 v2 v3 v4
[1,] -2.47148729 0.61727115 1.28285770 0.72974010
[2,] 0.42657513 0.77615280 1.88207246 0.41295301
[3,] -0.32480814 -1.75461602 -0.16589154 -0.52731722
[4,] 0.02760296 -2.08827618 -0.47176830 -0.17416765
[5,] -0.52760532 -0.20514629 0.15589594 -0.54623986
[6,] -0.47143259 -0.56666084 -1.35046101 -0.92754741
[7,] 0.61071291 -1.65132215 1.61024187 0.83128254
[8,] -0.17746888 -1.09887111 -0.32012303 0.69382341
[9,] -0.38707069 -0.69628506 0.04597653 0.13479181
[10,] 0.52030680 1.11764587 -1.10243994 -0.83949756
I'm thinking of having something like:
v1.v1 v1.v2 v1.v3 v1.v4 ... v4.v4
[1,] 0 1 2 5 ... 0
Like what cor(x) does when x is a matrix. I guess dplyr might be an answer? Or there is a multisample version pLepage()?
Consider using base R's sapply. Not familiar with LePage test in R, but using correlation and your example data:
rdmatrix <- cbind(v1=rnorm(10), v2=rnorm(10), v3=rnorm(10), v4=rnorm(10))
corrmatrix <- sapply(1:ncol(rdmatrix),
function(x,y) cor(rdmatrix[,x], rdmatrix[,y]), 1:ncol(rdmatrix))
# [,1] [,2] [,3] [,4]
# [1,] 1.0000000 -0.4613219 -0.5661391 -0.1703655
# [2,] -0.4613219 1.0000000 0.1965278 0.2111900
# [3,] -0.5661391 0.1965278 1.0000000 -0.3305471
# [4,] -0.1703655 0.2111900 -0.3305471 1.0000000
To flatten it out in a matrix of one row, consider the below using outer() for all combination set of column names and do.call(cbind, ...) to flatten:
# MATRIX OF ALL COLS PAIRINGS
cols <- outer(colnames(rdmatrix), colnames(rdmatrix),
function(y,x) paste0(x, '.', y)) # NOTICE INVERSION OF X AND Y
# FLATTEN COL NAMES
cols <- do.call(cbind, as.list(cols))
# FLATTEN CORR MATRIX DATA
finalmatrix <- do.call(cbind, as.list(corrmatrix))
# NAME MATRIX COLUMNS
colnames(finalmatrix) <- cols[1,]
# v1.v1 v1.v2 v1.v3 v1.v4
# [1,] 1 -0.4613219 -0.5661391 -0.1703655
# v2.v1 v2.v2 v2.v3 v2.v4
# [1,] -0.4613219 1 0.1965278 0.21119
# v3.v1 v3.v2 v3.v3 v3.v4
# [1,] -0.5661391 0.1965278 1 -0.3305471
# v4.v1 v4.v2 v4.v3 v4.v4
# [1,] -0.1703655 0.21119 -0.3305471 1
Data
I have a list of lists that looks something like this:
sublist1 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
sublist2 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
sublist3 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
mylist = list(sublist1,sublist2,sublist3)
My goal would be to pull out only the matrices named power
I've tried
mylist_power =mylist[sapply(mylist, '[', 'Power')]
But thats not working.
Brownie point alert!!!
How can I find the mean of the newly created list of matrices named power?
mylist_power <- sapply(mylist, '[', 'power')
and some means:
sapply(mylist_power, mean) # one per matrix
sapply(mylist_power, colMeans) # for each column and each matrix
sapply(mylist_power, rowMeans) # for each row and each matrix
mean(unlist(mylist_power)) # for the whole list
Reduce(`+`, mylist_power) / length(mylist_power) # element-wise
purrr solution which can be replicated to baseR's Map
#part 1 (to return only $power of every list item)
map(mylist, ~.x$power)
[[1]]
[,1]
[1,] 0.33281918
[2,] -1.12404046
[3,] -0.70613078
[4,] -0.72754386
[5,] -1.83431439
[6,] -0.40768794
[7,] 0.02686119
[8,] 0.91162864
[9,] 1.63434648
[10,] 0.06068561
[[2]]
[,1]
[1,] -0.02256943
[2,] -0.90315486
[3,] 0.90777295
[4,] 1.16194290
[5,] -0.45795340
[6,] 0.92795667
[7,] -2.10293514
[8,] -1.67716711
[9,] 1.76565577
[10,] 0.79444742
[[3]]
[,1]
[1,] -0.36200564
[2,] -1.13955016
[3,] -0.81537133
[4,] 1.31024563
[5,] -0.25836094
[6,] 0.60626489
[7,] 0.31344822
[8,] 0.05360308
[9,] 1.12825379
[10,] -0.55813346
part-2
map(mylist, ~.x$power %>% colMeans)
[[1]]
[1] -0.1833376
[[2]]
[1] 0.03939958
[[3]]
[1] 0.02783941
To get these values in a vector instead
map_dbl(mylist, ~.x$power %>% colMeans)
[1] -0.18333763 0.03939958 0.02783941