|
I really appreciate for all those quick responses.
It makes my life a little easier. Since the data I referred as a matrix is a
data frame (apology for the wrong information) the following answers would do
the job:
as.vector(unlist(data.mat)): Bill Dunlap,
Chuck Cleland, Jean Adams,Buttrey Samuel and Matthew
Soukup provided
as.vector(as.matrix(data.mat)): David Smith, Brian
Ripley and Peter Alspach suggested
c(as.matrix(data.mat)): Buttrey
Samuel.
Tim Hesterberg and Chasalow Scott mentioned about a
interesting subject and I pasted their posts below.
Tim Hesterberg:
If data.mat (a dataframe) contains any columns of
mode "character", >as.matrix (at least in Splus 4.5 and Splus 2000 for
Windows) will call >format() to give all elements of the column the same
number of characters >(by whitespace-padding). Yet another
characters-in-dataframe-related >feature that can be very
annoying.
In Splus6, as.matrix.data.frame has an optional argument
`justify.format' that lets you prevent this.
> temp _
data.frame(x=c("a", "b", "c"), y=c(1,10,100)) > as.matrix(temp) #
default justify.format is "decimal"
x y 1 "a" " 1" 2 "b" " 10" 3 "c"
"100" > as.matrix(temp, justify.format = "none")
x y 1 "a" "1" 2 "b" "10" 3 "c"
"100"
In earlier versions you could create your own copy of
as.matrix.data.frame with this added argument, that passes the argument to
format.
>...If you have factor columns AND character columns,
you >might have to work harder to make them all come out the way you
want.
Note that there are three functions designed to convert a data
frame into a matrix, that differ in how they handle character and factor
columns; one of these may do what you want:
Column
type as.matrix.data.frame()
data.matrix()
numerical.matrix() factor
character
codes NA (or
removed) character
character
character NA (or removed)
Chasalow Scott:
If data.mat (a dataframe) contains any columns of
mode "character", as.matrix (at least in Splus 4.5 and Splus 2000 for
Windows) will call format() to give all elements of the column the same
number of characters (by whitespace-padding). Yet another
characters-in-dataframe-related feature that can be very annoying. To
avoid this in this application, you could do, I
believe,
unlist(data.mat, use.names = F)
instead. On the
other hand, if data.mat contains any factor columns, unlist() will give you
the factor codes, as.matrix will give you the factor labels. Your
choice. If you have factor columns AND character columns, you might
have to work harder to make them all come out the way you
want.
For matrix:
as.vector(data.mat): Bill Venables, Kebin Brand,
Carlson Deborah and James Holtman
dim(data.mat)_NULL: Giovanni Petris and Hong
Ooi
c(data.mat):Jim stapleton suggested.
Gibiansky Leonid suggested a function:
{
data.vec[kb:kn]
<-data.mat[i,1:400]
kb<-kb+400
ke<-ke+400
}
}
Also Dennis Murphy:
mat2vec function(x,byrow=FALSE) # use F rather
than FALSE in S-Plus { if(!is.matrix(x))
stop("Object is not a matrix") if(byrow) x <-
t(x) dim(x) <- NULL x
}
I hope I listed all the names who answered my
question.
Kyong US
Army
|