[R] Converting dataframe to matrix
Richard O'Keefe
r@oknz @end|ng |rom gm@||@com
Thu Jun 26 09:47:22 CEST 2025
CAN you use as.matrix(dat) to convert your data frame to a matrix?
Yes, certainly. Try it!
SHOULD you do this?
Well, why do you WANT a matrix? Data frames act a lot like
two-dimensional arrays as they are.
If "the values are basically categories", then perhaps they should BE
factors and processed as factors.
For example, what meaning do you expect colMeans(dat) to have?
On Thu, 26 Jun 2025 at 12:25, Daniel Lobo <danielobo9976 using gmail.com> wrote:
>
> My data frame looks like below
>
> dat = structure(list(a = c(66, 100, 100, 100, 100, 100, 100, 66, 100,
>
> 66), b = c(100, 50, 100, 100, 100, 100, 100, 100, 100, 100),
>
> c = c(75, 25, 75, 50, 50, 50, 50, 50, 75, 25)), class =
> "data.frame", row.names = c(NA,
>
> -10L))
>
> The values are basically categories for each column, however there may
> be missing values present, which typically represented as NA.
>
> My question is can I directly use as.matrix(na.omit(dat)) to convert
> this to matrix?
>
>
> On Thu, 26 Jun 2025 at 05:27, Rolf Turner <rolfturner using posteo.net> wrote:
> >
> >
> > On Thu, 26 Jun 2025 03:45:50 +0530
> > Daniel Lobo <danielobo9976 using gmail.com> wrote:
> >
> > > Hi,
> > >
> > > I have a dataframe for which all columns are numeric but categorical.
> >
> > I don't understand what that means. Perhaps an example?
> >
> > > There are some missing values as well
> > >
> > > Typically, I have CSV file saved in drive, and then read it using
> > > read.csv command
> >
> > Is that relevant?
> >
> > > Can I use as.matrix(na.omit(<<my dataframe loaded using read.csv>>))
> > > to convert such dataframe to matrix?
> > >
> > > I there any data loss or change that may occur if I use as.matrix
> > > command?
> >
> > I think your question is too vague for anyone to be able to answer this.
> >
> > > I remember that some experts recommends not to use as.matrix()
> > > command to convert a dataframe to matrix.
> >
> > My guess is that the problem is that as.matrix() will coerce all of the
> > columns of a data frame to a common class, which might yield unexpected
> > results.
> >
> > > Any guidance will be very helpful.
> >
> > It's possible that data.matrix() might be useful.
> >
> > But basically you should think carefully about what the nature of the
> > entries of your data frame could possibly be, and then design your code
> > to accommodate all of these possibilities, throwing an error if any
> > entry does not conform to any of the possibilities that you envisage.
> >
> > cheers,
> >
> > Rolf Turner
> >
> > --
> > Honorary Research Fellow
> > Department of Statistics
> > University of Auckland
> > Stats. Dep't. (secretaries) phone:
> > +64-9-373-7599 ext. 89622
> > Home phone: +64-9-480-4619
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list