[R] Populating then sorting a matrix and/or data.frame

Michael Bedward michael.bedward at gmail.com
Thu Nov 11 08:16:48 CET 2010


Hello Noah,

If you set these names...
> names(results) <- c("one", "two", "three")

this won't work...
> results[results$c < 100,]

because you don't have a column called "c" (unless that's just a typo
in your post).

> I tried making it a data.frame with
> foo <- data.frame(results)
>
> But that converted all the numeric values to factors!!!

Not sure what's going on there. If 'results' is a numeric matrix you
should get a data.frame with numeric cols since under the hood this is
just calling the as.data.frame function.

Michael





On 11 November 2010 16:02, Noah Silverman <noah at smartmediacorp.com> wrote:
> Hi,
>
> I have a process in R that produces a lot of output.  My plan was to build
> up a matrix or data.frame "row by row", so that I'll have a nice object with
> all the resulting data.
>
> I started with:
> results <- matrix(ncol=3)
> names(results) <- c("one", "two", "three")
>
> Then, when looping through the data:
> results <- rbind(results, c(a,b,c))
>
> This seems to work fine. BUT, my problem arises when I want to filter, sort,
> etc.
>
> I tried (thinking like a data.frame):
> results[results$c < 100,]
>
> But that fails.
>
> I tried making it a data.frame with
> foo <- data.frame(results)
>
> But that converted all the numeric values to factors!!!  Which causes a
> whole mess of problems.
>
> Any ideas??
>
> -N
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list