Hi I observed an interesting behavior of R. Can you find where is the bug, or it is not a bug but made deliberately. - Hide quoted text -> arr = c(); #defined the empty array > a= c("x1", "x2"); > b = c("y1", "y2"); > arr = rbind(arr,a); #row bind the first character array -a > arr = rbind(arr,b); # row bind the second character array-bEverything ok upto this point, arr content is displayed as follows> arr[,1] [,2] a "x1" "x2" b "y1" "y2" Now I delete any row: arr = arr[-1,]; The value of arr is :> arr[1] "y1" "y2" Problem: I want to access the first row now using:>arr[1, ]Error in arr[1, 1] : incorrect number of dimensions Though it is showing the value as under:> arr[1][1] "y1"> arr[2][1] "y2" I think, when there is single row, R is considering it as an array and not as matrix. But why it is so?????????????/ -Fahim -- View this message in context: http://n4.nabble.com/strange-behavior-of-R-tp1010047p1010047.html Sent from the R help mailing list archive at Nabble.com.
You need to reread the help for [, specifically the drop argument. ?"[" Sarah On Fri, Jan 8, 2010 at 5:57 PM, Fahim <fahim.md at gmail.com> wrote:> > Hi > I observed an interesting behavior of R. Can you find where is the bug, or > it is not a bug but made deliberately. > - Hide quoted text - > > >> arr = c(); ? ? ? ? ? ? ? ? ? ? ? ?#defined the empty array >> a= c("x1", "x2"); >> b = c("y1", "y2"); >> arr = rbind(arr,a); ? ? ? ? ? ?#row bind the first character array -a >> arr = rbind(arr,b); ? ? ? ? ? ?# row bind the second character array-b > > Everything ok upto this point, arr content is displayed as follows >> arr > ?[,1] [,2] > a "x1" "x2" > b "y1" "y2" > > Now I delete any row: > arr = arr[-1,]; > > The value of arr is : >> arr > [1] "y1" "y2" > > Problem: I want to access the first row now using: >>arr[1, ] > Error in arr[1, 1] : incorrect number of dimensions > > Though it is showing the value ?as under: >> arr[1] > [1] "y1" > >> arr[2] > [1] "y2" > > > I think, when there is single row, R is considering it as an array and not > as matrix. But why it is so?????????????/ > >-- Sarah Goslee http://www.functionaldiversity.org
On Jan 8, 2010, at 5:57 PM, Fahim wrote:> > Hi > I observed an interesting behavior of R. Can you find where is the > bug, or > it is not a bug but made deliberately. > - Hide quoted text - > > >> arr = c(); #defined the empty array >> a= c("x1", "x2"); >> b = c("y1", "y2"); >> arr = rbind(arr,a); #row bind the first character array -a >> arr = rbind(arr,b); # row bind the second character >> array-b > > Everything ok upto this point, arr content is displayed as follows >> arr > [,1] [,2] > a "x1" "x2" > b "y1" "y2" > > Now I delete any row: > arr = arr[-1,]; > > The value of arr is : >> arr > [1] "y1" "y2" > > Problem: I want to access the first row now using: >> arr[1, ] > Error in arr[1, 1] : incorrect number of dimensionsuse arr[ , -1, drop=FALSE] to avoid loosing dimensions. ?"[" -- David.> > Though it is showing the value as under: >> arr[1] > [1] "y1" > >> arr[2] > [1] "y2" > > > I think, when there is single row, R is considering it as an array > and not > as matrix. But why it is so?????????????/ >Because you didn't red the manual. David Winsemius, MD Heritage Laboratories West Hartford, CT
On Fri, Jan 8, 2010 at 10:57 PM, Fahim <fahim.md at gmail.com> wrote:>> arr > [1] "y1" "y2" >At this moment 'arr' no longer has two dimensions, but only one. So you can access it only as a vector.> Problem: I want to access the first row now using: >>arr[1, ] > Error in arr[1, 1] : incorrect number of dimensions >Correct. It has only one dim.> Though it is showing the value ?as under: >> arr[1] > [1] "y1" > >> arr[2] > [1] "y2" >This is how you would access individual elements in vectors. Liviu