简体   繁体   中英

Loop through list or a matrix in R

Mfrq is a matrix of 608 x 10 and list.ids is a list of 608 users with 10 items in each. I am trying to find the distance between every two rows in the matrix or every two vectors in the list. I am expecting to have an output as a matrix of 608 x 608 with distances in it. The attached code is I have been running & the errors I am receiving.

<This is for the list>
rnext=row+1
for (row in 1:length(list.ids)){
  Wdist=as.matrix(wasserstein1d(list.ids[[row]],list.ids[[rnext]]))
  row=rnext
}

This one works but it only gives me a single value.

<the value>
 Wdist
         [,1]
[1,] 2.585177

For the matrix I have:

i=1:nrow(Mfrq)
sapply(Mfrq, function(x) as.matrix({wasserstein1d(x[i,],x[i+1,])}) )

with the error of

Error in x[i, ] : incorrect number of dimensions

My matrix looks like

head(Mfrq)
             [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]       [,9]
1029891  3.703704  6.172840  8.641975 24.691358 39.506173  4.938272  4.938272  3.703704  1.2345679
1044051  4.347826  8.695652  8.695652 21.739130 26.086957  4.347826 13.043478  0.000000  8.6956522
1056991  1.538462  0.000000 10.769231  4.615385  7.692308  9.230769  9.230769 33.846154 13.8461538
1069151  0.000000  0.000000  3.703704  3.703704  7.407407 14.814815 11.111111  7.407407 11.1111111
1071801  4.761905 19.047619 14.285714 23.809524  4.761905  9.523810  4.761905  9.523810  4.7619048
1072191 31.417625 29.118774 13.026820  9.578544  9.195402  3.831418  1.149425  1.149425  0.7662835
             [,10]
1029891  2.4691358
1044051  4.3478261
1056991  9.2307692
1069151 40.7407407
1071801  4.7619048
1072191  0.7662835

and the list looks like

$`1029891`
 [1]  3.703704  6.172840  8.641975 24.691358 39.506173  4.938272  4.938272  3.703704  1.234568  2.469136

$`1044051`
 [1]  4.347826  8.695652  8.695652 21.739130 26.086957  4.347826 13.043478  0.000000  8.695652  4.347826

$`1056991`
 [1]  1.538462  0.000000 10.769231  4.615385  7.692308  9.230769  9.230769 33.846154 13.846154  9.230769

$`1069151`
 [1]  0.000000  0.000000  3.703704  3.703704  7.407407 14.814815 11.111111  7.407407 11.111111 40.740741

$`1071801`
 [1]  4.761905 19.047619 14.285714 23.809524  4.761905  9.523810  4.761905  9.523810  4.761905  4.761905

$`1072191`
 [1] 31.4176245 29.1187739 13.0268199  9.5785441  9.1954023  3.8314176  1.1494253  1.1494253  0.7662835
[10]  0.7662835

There are a few problems here. First, if you are using a for loop, you should not change the iterated variable. You have:

for (row in 1:length(list.ids)){
  Wdist=as.matrix(wasserstein1d(list.ids[[row]],list.ids[[rnext]]))
  row=rnext
}

You should not have row = rnext in this loop.

Question: Do you need to calculate the distances between every two consecutive rows? Or, do you need to calculate them for every combination of two rows? These two are different.

If you want to calculate the distances between every two consecutive rows, then your row variable must run from 1 to 9, and rnext variable must run from 2 to 10.

Your second problem is that you are not saving anything in this loop:

for (row in 1:length(list.ids)){
  Wdist=as.matrix(wasserstein1d(list.ids[[row]],list.ids[[rnext]]))
  row=rnext
}

You are overwriting Wdist in every iteration.

You have similar problems for the matrix part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM