This is what I tried:
> num.vec <- c(12.34,56.78,90.12,34.56)
> names(num.vec)<-c("first","second","third","fourth")
> num.vec
first second third fourth
12.34 56.78 90.12 34.56
> seq<-(1:4)
> num.mat<-rbind(num.vec,seq)
> num.mat
first second third fourth
num.vec 12.34 56.78 90.12 34.56
seq 1.00 2.00 3.00 4.00
> num.vec [3:4]
third fourth
90.12 34.56
(until here I'm fine)
> num.mat [seq]
[1] 12.34 1.00 56.78 2.00
> num.mat [num.vec]
[1] NA NA NA NA
> num.vec [seq]
first second third fourth
12.34 56.78 90.12 34.56
> num.mat [num.vec]
[1] NA NA NA NA
> num.mat [-seq]
[1] 90.12 3.00 34.56 4.00
(and here I'm lost!)
How could I display a row, instead of always seemingly falling back to
columns?
Uwe
Explanation w.r.t. rbind, please!
4 messages · Gavin Simpson, Meyners, Michael, LAUSANNE, AppliedMathematics, Uwe Dippel
On Fri, 2010-01-29 at 18:56 +0800, Uwe Dippel wrote:
This is what I tried:
> num.vec <- c(12.34,56.78,90.12,34.56)
> names(num.vec)<-c("first","second","third","fourth")
> num.vec
first second third fourth 12.34 56.78 90.12 34.56
> seq<-(1:4) > num.mat<-rbind(num.vec,seq) > num.mat
first second third fourth num.vec 12.34 56.78 90.12 34.56 seq 1.00 2.00 3.00 4.00
> num.vec [3:4]
third fourth 90.12 34.56 (until here I'm fine)
> num.mat [seq]
[1] 12.34 1.00 56.78 2.00
> num.mat [num.vec]
[1] NA NA NA NA
> num.vec [seq]
first second third fourth 12.34 56.78 90.12 34.56
> num.mat [num.vec]
[1] NA NA NA NA
> num.mat [-seq]
[1] 90.12 3.00 34.56 4.00 (and here I'm lost!) How could I display a row, instead of always seemingly falling back to columns?
You're indexing the matrix as if it were a vector. Which is fine, except that in R matrices are indexed by columns, hence the last result your got - you dropped the first 4 entries which are the first two columns, hence you got the last two columns of data. Knowing this you could alter your seq to be seq(1, 8, by = 2):
num.mat[seq(1, 8, by = 2)]
[1] 12.34 56.78 90.12 34.56 Thank full there is an easier way: ?`[` has the details, look at arguments i and j.
num.mat[1,]
first second third fourth 12.34 56.78 90.12 34.56 or, without the (col)names
unname(num.mat[1,])
[1] 12.34 56.78 90.12 34.56 HTH G
Uwe
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Uwe Dippel Sent: Freitag, 29. Januar 2010 11:57 To: r-help at r-project.org Subject: [R] Explanation w.r.t. rbind, please! This is what I tried:
> num.vec <- c(12.34,56.78,90.12,34.56) >
names(num.vec)<-c("first","second","third","fourth")
> num.vec
first second third fourth 12.34 56.78 90.12 34.56
> seq<-(1:4) > num.mat<-rbind(num.vec,seq) > num.mat
first second third fourth num.vec 12.34 56.78 90.12 34.56 seq 1.00 2.00 3.00 4.00
> num.vec [3:4]
third fourth 90.12 34.56 (until here I'm fine)
> num.mat [seq]
[1] 12.34 1.00 56.78 2.00
What you (probably) want here is num.mat ["seq",]
> num.mat [num.vec]
[1] NA NA NA NA
num.mat["num.vec",] and so on. You have to use tell R that you want the ROW (that's why the comma is needed) defined by the NAME "seq" or "num.vec" (that's why you need "") . Otherwise, R replaces seq by its value 1:4, so num.mat[seq] is identical to num.mat[1:4] (and num.mat[12.34] is NA of course in num.mat[num.vec]) You should have a closer look at ?Extract HTH, Michael
> num.vec [seq]
first second third fourth 12.34 56.78 90.12 34.56
> num.mat [num.vec]
[1] NA NA NA NA
> num.mat [-seq]
[1] 90.12 3.00 34.56 4.00 (and here I'm lost!) How could I display a row, instead of always seemingly falling back to columns? Uwe
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Meyners,Michael,LAUSANNE,AppliedMathematics wrote:
What you (probably) want here is num.mat ["seq",]
> num.mat [num.vec]
[1] NA NA NA NA
num.mat["num.vec",] and so on. You have to use tell R that you want the ROW (that's why the comma is needed) defined by the NAME "seq" or "num.vec" (that's why you need "") .
Thanks, I had been exactly there before, and it didn't work neither. Now it works. I do understand the need for the rows *now*. Uwe