Reputation: 97
I am trying to collect an entire array of data (hours 0, 3, 6...48). My code currently only collects hh = 0 because the "name" of the row has a comma in it.
The data in tempjson$score_value is structured as follows:
tempjson$score_value
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3.492809 2.962649 2.770052 2.756788 2.332905 2.338647 2.719658 3.080747
[2,] 3.558785 3.581791 3.475497 3.451713 3.009123 3.104995 3.397145 3.781619
etc.
I am trying to plot the score value for each hour (each column) for both experiments (both rows).
tempjson$score_value[1]
[1] 3.492809
only selects the very first element, but
> tempjson$score_value[1,]
[1] 3.492809 2.962649 2.770052 2.756788 2.332905 2.338647 2.719658 3.080747
3.406330 3.281600 3.028505 2.976836 2.575598 2.421316 2.894790 3.132405
3.541107
provides the entire row. My code is a for loop that tries to compile a tempscore array of all the hours' values for both experiments [2x17]:
for(exp in seq(1,length(expname)))
for(hh in tempjson$label[[exp]])
tempscore[exp,which(list.runhour==hh)]=tempjson$score_value[[exp]][which(tempjson$label[[exp]]==hh)]
where:
> expname
[1] "experiment1" "experiment2"
and
> list.runhour
[1] "0" "3" "6" "9" "12" "15" "18" "21" "24" "27" "30" "33" "36" "39" "42"
[16] "45" "48"
and
> tempjson$label
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] "0" "3" "6" "9" "12" "15" "18" "21" "24" "27" "30" "33" "36" "39"
[2,] "0" "3" "6" "9" "12" "15" "18" "21" "24" "27" "30" "33" "36" "39"
[,15] [,16] [,17]
[1,] "42" "45" "48"
[2,] "42" "45" "48"
and
> tempjson$score_value
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3.492809 2.962649 2.770052 2.756788 2.332905 2.338647 2.719658 3.080747
[2,] 3.558785 3.581791 3.475497 3.451713 3.009123 3.104995 3.397145 3.781619
[,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] 3.406330 3.28160 3.028505 2.976836 2.575598 2.421316 2.894790 3.132405
[2,] 4.163044 3.89846 3.626449 3.615926 3.260005 3.190443 3.648776 3.873211
[,17]
[1,] 3.541107
[2,] 4.256343
Running this code results, from line 1, in exp = [1] and not [1,]. As such, hh only ever = "0" and not the other 16 values [1x17 array from 0 to 48].
How do I get exp to equal the name of the row, i.e., [1,], so that the for loop correctly calls all the columns (hhs)?
Upvotes: 4
Views: 234
Reputation: 886928
We can use the ,
to separate the row index from the column index. With matrix
, if we use only a single index 'n', it will extract the 'n'th element as matrix
is a vector
with dim
attributes
tempjson$score_value[1, , drop = FALSE]
Here, the drop = FALSE
is used to avoid the matrix dropping the dimensions when the number of rows or columns is 1. It is explained in ?Extract
Upvotes: 1