ian ian - 2 months ago 5
R Question

Extracting individual readings from array by useing column and row names

If I have part of an array extracted as below named "Testrun" of temperatures
at global coordinates as below

88.54 86.65 84.75 82.85

0 245.93 245.50 243.67 246.60
1.88 245.93 245.54 244.03 246.94
3.75 245.94 245.58 244.39 247.28
5.62 245.94 245.62 244.75 247.61
7.5 245.95 245.66 245.11 247.95


I named the columns be degrees of longitude and the rows by degrees of latitude for the whole array before extracting "Testrun".

I can now extract any individual value by the following

> Testrun[3,2,1]
[1] 245.58


But I can't get the same result using the column and row names

>
Testrun[3.75,86.5,1]
Error in Testrun[3.75, 86.5, 1] : subscript out of bounds


Surly that can be done , so what am I doing wrong?

rbm rbm
Answer

OK, without seeing the data im assuming you have a multi-dim data, something like

data <- array(1:27, dim=c(3,3,3), dimnames = list(11:13,11:13,11:13))

in order to access the element at position (2,2,2) you can do

data[2,2,2]

but accessing using dimnames you can't do

data[12,12,12]
Error in data[12, 12, 12] : subscript out of bounds

as R thinks it's 12th element. You need to wrap it in quotes, i.e.

data["12","12","12"]
[1] 14
Comments