# vector entry in matix

8 messages
Open this post in threaded view
|

## 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!
Open this post in threaded view
|

## Re: vector entry in matix

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: vector entry in matix

 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?
Open this post in threaded view
|

## Re: vector entry in matix

 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.htmlSent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: vector entry in matix

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|