[R] vector entry in matix

arun
Thu, 05 Jul 2012 16:57:42 -0000

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. <thomas.csapak at gmail.com>
To: r-help at r-project.org
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.

______________________________________________
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.