vector entry in matix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

vector entry in matix

Thomas C.
hi,

i'm trying to figure out if there's any possibility to write a whole vector into a matrix or data.frame or something like that. i don't mean transormation. Here an example:

       [,1]  [,2]
[1,] "a" "d"
[2,] "b" "e"
[3,] "c" "f"


where e.g.  a is a<-c(0,1) vector of length 2, b a vector of length 4,... (i know that the matrix enties are strings :)). i'm trying to put some variables into a matrix or data.frame, but i couldn't handle it. is something like that possible in R?

thanks for your help!
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

Michael Weylandt
It is possible to put dimensionality on a list (i.e., a generic
vector), which might be what you're looking for.

x <- list(1:4, letters[1:4], function(x,y) x + y, rnorm(50))
dim(x) <- c(2,2)

x[[1,2]]

x[[2,2]]

x[[3,2]] # Error

Best,
Michael

On Thu, Jul 5, 2012 at 8:19 AM, Thomas C. <[hidden email]> wrote:

> hi,
>
> i'm trying to figure out if there's any possibility to write a whole vector
> into a matrix or data.frame or something like that. i don't mean
> transormation. Here an example:
>
>        [,1]  [,2]
> [1,] "a" "d"
> [2,] "b" "e"
> [3,] "c" "f"
>
>
> where e.g.  a is a<-c(0,1) vector of length 2, b a vector of length 4,... (i
> know that the matrix enties are strings :)). i'm trying to put some
> variables into a matrix or data.frame, but i couldn't handle it. is
> something like that possible in R?
>
> thanks for your help!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/vector-entry-in-matix-tp4635478.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

Thomas C.
well thank you, i think we get closer to my problem. but i meant it in a different way. maybe i describe what i intended to do:

i have number of triangles which i'd like to store in a list, matrix,...etc. i thought it could look sth. like that:

triangle        node1            node2            node3   .....
     1               c(x1,y1)         c(x2,y3)          c(x3,y3)
     1               c(x4,y4)         c(x5,y5)          c(x6,y6)
.
.
.

i know that i could just make more rows for the x/y values but the problem is, that i need to store more than these properties (say the neighbours of these nodes and their coordinates) and i thought it would be more convenient to save these as vectors. it was just a thought... is this possible?

Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

arun kirshna
In reply to this post by Thomas C.
Hi,
I think it is better to store vectors in to list when the vectors are of different lengths.

#Consider these cases,
#vectors of equal length

set.seed(1)
 list1<-list(a=rnorm(5,15),b=rnorm(5,25),c=1:5,d=runif(5,0.4),e=16:20,f=rep(1,5))

mat1<-cbind(rbind(list1[[1]],list1[[2]],list1[[3]]),(rbind(list1[[4]],list1[[5]],list1[[6]])))
mat1
         [,1]     [,2]     [,3]     [,4]     [,5]       [,6]       [,7]
[1,] 16.43302 16.98040 14.63278 13.95587 15.56972  0.5371949  0.7574272
[2,] 24.86495 27.40162 24.96076 25.68974 25.02800 16.0000000 17.0000000
[3,]  1.00000  2.00000  3.00000  4.00000  5.00000  1.0000000  1.0000000
           [,8]       [,9]      [,10]
[1,]  0.7449233  0.4462386  0.4213243
[2,] 18.0000000 19.0000000 20.0000000
[3,]  1.0000000  1.0000000  1.0000000

dat1<-data.frame(list1)

dat1
         a        b c         d  e f
1 16.43302 24.86495 1 0.5371949 16 1
2 16.98040 27.40162 2 0.7574272 17 1
3 14.63278 24.96076 3 0.7449233 18 1
4 13.95587 25.68974 4 0.4462386 19 1
5 15.56972 25.02800 5 0.4213243 20 1
#vectors of uneual length
#works with list

list2<-list(a=rnorm(5,15),b=rnorm(10,25),c=5:15,d=runif(3,0.4),e=1:5,f=rep(1:3,3))
> list2
$a
[1] 15.36594 15.24841 15.06529 15.01916 15.25734

$b
 [1] 24.35099 24.88083 25.66414 26.10097 25.14377 24.88225 24.08793 23.56241
 [9] 24.20291 26.25408

$c
 [1]  5  6  7  8  9 10 11 12 13 14 15

$d
[1] 0.8679909 0.9283714 0.6478745

$e
[1] 1 2 3 4 5

$f
[1] 1 2 3 1 2 3 1 2 3


#Warning or Errors in matrix and dataframe
mat2<-cbind(rbind(list2[[1]],list2[[2]],list2[[3]]),(rbind(list2[[4]],list2[[5]],list2[[6]])))
Warning messages:
1: In rbind(list2[[1]], list2[[2]], list2[[3]]) :
  number of columns of result is not a multiple of vector length (arg 1)
2: In rbind(list2[[4]], list2[[5]], list2[[6]]) :
  number of columns of result is not a multiple of vector length (arg 2)


  dat2<-data.frame(list2)
Error in data.frame(a = c(15.3659411230492, 15.2484126488726, 15.0652881816716,  :
  arguments imply differing number of rows: 5, 10, 11, 3, 9

#So, it would be better to store it as a list.

#But, you can still convert vectors of unequal lengths to data frame or matrix with NAs.
library(zoo)
a<-zoo(1:8,1:14)
b<-zoo(5:8,3:5)
c<-zoo(1:5,2:5)
 list3<-list("a","b","c")
 z<-zoo()
 for(i in 1:length(list3)) z<-merge(z,get(list3[[i]]))
 names(z)<-unlist(list3)
 z
   a  b  c
1  1 NA NA
2  2 NA  1
3  3  5  2
4  4  6  3
5  5  7  4
6  6 NA NA
7  7 NA NA
8  8 NA NA
9  1 NA NA
10 2 NA NA
11 3 NA NA
12 4 NA NA
13 5 NA NA
14 6 NA NA
 z1<-data.frame(z)
is.data.frame(z1)
[1] TRUE
 str(z1)
'data.frame':    14 obs. of  3 variables:
 $ a: int  1 2 3 4 5 6 7 8 1 2 ...
 $ b: int  NA NA 5 6 7 NA NA NA NA NA ...
 $ c: int  NA 1 2 3 4 NA NA NA NA NA ...
z2<-as.matrix(z)

A.K.




----- Original Message -----
From: Thomas C. <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, July 5, 2012 9:19 AM
Subject: [R] vector entry in matix

hi,

i'm trying to figure out if there's any possibility to write a whole vector
into a matrix or data.frame or something like that. i don't mean
transormation. Here an example:

       [,1]  [,2]
[1,] "a" "d"
[2,] "b" "e"
[3,] "c" "f"


where e.g.  a is a<-c(0,1) vector of length 2, b a vector of length 4,... (i
know that the matrix enties are strings :)). i'm trying to put some
variables into a matrix or data.frame, but i couldn't handle it. is
something like that possible in R?

thanks for your help!

--
View this message in context: http://r.789695.n4.nabble.com/vector-entry-in-matix-tp4635478.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

David Winsemius
In reply to this post by Michael Weylandt

On Jul 5, 2012, at 11:22 AM, R. Michael Weylandt wrote:

> It is possible to put dimensionality on a list (i.e., a generic
> vector), which might be what you're looking for.
>
> x <- list(1:4, letters[1:4], function(x,y) x + y, rnorm(50))
> dim(x) <- c(2,2)
>
> x[[1,2]]
>
> x[[2,2]]
>
> x[[3,2]] # Error

That constructs a 2 x 2 matrix whose elements are lists:

 > x
      [,1]        [,2]
[1,] Integer,4   ?
[2,] Character,4 Numeric,50
 > is.matrix(x)
[1] TRUE

I suppose one could take that approach to constructing an Excel-mimic,  
since the functional element survives that passage:

 > x[1,2][[1]](3,4)
[1] 7


>
> Best,
> Michael
>
> On Thu, Jul 5, 2012 at 8:19 AM, Thomas C. <[hidden email]>  
> wrote:
>> hi,
>>
>> i'm trying to figure out if there's any possibility to write a  
>> whole vector
>> into a matrix or data.frame or something like that. i don't mean
>> transormation. Here an example:
>>
>>       [,1]  [,2]
>> [1,] "a" "d"
>> [2,] "b" "e"
>> [3,] "c" "f"
>>
>>
>> where e.g.  a is a<-c(0,1) vector of length 2, b a vector of length  
>> 4,... (i
>> know that the matrix enties are strings :)). i'm trying to put some
>> variables into a matrix or data.frame, but i couldn't handle it. is
>> something like that possible in R?

The matrix classes objects loose their names, however. You will not  
have a,b, ... as handles in that matrix. You could create row and  
column names.

>>
>> thanks for your help!
>>


David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

Bert Gunter
In reply to this post by Thomas C.
You have failed to provide a complete, coherent description of what you
wish to do. In the absence of such a description, all suggestions are just
guesses. You need to think carefully about what information you want to
associate with each triangle and the appropriate data structure to use to
do this. You might do well to talk with a computer scientist about this. An
object-oriented framework seems like the natural approach. This also sounds
like the sort of thing Bioconductor might already have, so search around
there, perhaps posting on their Help list.

-- Bert

On Thu, Jul 5, 2012 at 9:37 AM, Thomas C. <[hidden email]> wrote:

> well thank you, i think we get closer to my problem. but i meant it in a
> different way. maybe i describe what i intended to do:
>
> i have number of triangles which i'd like to store in a list,
> matrix,...etc.
> i thought it could look sth. like that:
>
> triangle        node1            node2            node3   .....
>      1               c(x1,y1)         c(x2,y3)          c(x3,y3)
>      1               c(x4,y4)         c(x5,y5)          c(x6,y6)
> .
> .
> .
>
> i know that i could just make more rows for the x/y values but the problem
> is, that i need to store more than these properties (say the neighbours of
> these nodes and their coordinates) and i thought it would be more
> convenient
> to save these as vectors. it was just a thought... is this possible?
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/vector-entry-in-matix-tp4635478p4635509.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

Quigi
In reply to this post by Thomas C.
To second Bert Gunter: you may get better answers if you give us a complete
description.

On Thu, Jul 5, 2012 at 12:37 PM, Thomas C. <[hidden email]> wrote:

> i have number of triangles which i'd like to store in a list,
> matrix,...etc.
> i thought it could look sth. like that:
>
> triangle        node1            node2            node3   .....
>      1               c(x1,y1)         c(x2,y3)          c(x3,y3)
>      1               c(x4,y4)         c(x5,y5)          c(x6,y6)
>

You already indicate that that's not the whole truth, but if it was you
could consider one of two approaches:
* Rather than a 2D array (also called "matrix") use a 3D array, with
dimensions n.triangle x 3 x 2.
* You could use a n.triangle x 3 matrix of complex numbers.  That may look
like a sneaky trick to put a vector into a matrix element, but some
operations of planar geometry can written elegantly.  E.g., distance
between a and b is abs(a-b).

It all depends on what you want to do.  BTW, what do you consider a
neighbor of a triangle?
*
*
If you are working with a triangle mesh, it might be better to hold the
vertices in a double matrix (n.vertex x 2) and represent the triangles as
indices into it, i.e., as an integer matrix (n.triangle x 3).  You may use
more columns in either matrix to represent more attributes.  If you find
out you need columns of different types, consider a data frame.

/Christian

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: vector entry in matix

Joshua Wiley-2
In reply to this post by Thomas C.
Hi Thomas,

This is non trivial to do, but if you will be working with this sort
of data and are inclined to do some programming, you might consider
creating a new class.  S4 classes and methods are quite flexible, and
you can allow them to lean on or inherit from existing classes such as
matrices.  For example, your class might consist of three lists and a
data frame.  The length of each list would be forced to be equal to
the number of rows in the data frame.  Each element of each list would
support a length 2 numeric vector (or integer vector or whatever it is
you need).  THe lists then would hold nodes 1 - 3, and the data frame
would hold other information.  You can similarly write methods so that
for your special class,

d[1, 1] would return c(x1, y1), essentially you create a mix of lists
and a matrix with all the necessary constraints, and you develop
methods that allow you to operate on this in a way that is sensible
for your data.

This is stab in the dark, but if you happen to be dealing with social
network style data, (triangles and neighbors make me think of that),
you should investigate existing packages for that as they may already
have their own classes/have a way they expect data to be structured.
Off the top of my head, the package "sna" comes to mind as a starting
place to look (though I am not personally very familiar with it).

Cheers,

Josh

On Thu, Jul 5, 2012 at 9:37 AM, Thomas C. <[hidden email]> wrote:

> well thank you, i think we get closer to my problem. but i meant it in a
> different way. maybe i describe what i intended to do:
>
> i have number of triangles which i'd like to store in a list, matrix,...etc.
> i thought it could look sth. like that:
>
> triangle        node1            node2            node3   .....
>      1               c(x1,y1)         c(x2,y3)          c(x3,y3)
>      1               c(x4,y4)         c(x5,y5)          c(x6,y6)
> .
> .
> .
>
> i know that i could just make more rows for the x/y values but the problem
> is, that i need to store more than these properties (say the neighbours of
> these nodes and their coordinates) and i thought it would be more convenient
> to save these as vectors. it was just a thought... is this possible?
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/vector-entry-in-matix-tp4635478p4635509.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.



--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

______________________________________________
[hidden email] 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.