# Re: [R] problems with data frames, factors and lists

From: jim holtman <jholtman_at_gmail.com>
Date: Wed, 21 May 2008 13:12:01 -0400

Try this:

doset <- function(sameset) {

do.call(rbind, lapply(unique(c(sameset\$APID, sameset\$BPID)),

function(.pid) mix(sameset, .pid))) }

On Wed, May 21, 2008 at 11:33 AM, Karin Lagesen <karinlag_at_studmed.uio.no> wrote:

>
> I have a function that creates a list based on some clustered data:
>
> mix <- function(Y, pid) {
> hc = gethc(Y,pid)
> maxheight = max(hc\$height)
> noingrp = processhc(hc)
> one = noingrp\$one
> two = noingrp\$two
> twoisone = "one"
> if (two != 1)
> twoisone = "more"
> out = list(pid = pid,one = noingrp\$one, two = noingrp\$two, diff =
> maxheight, noseqs = length(hc\$labels), twogrp = twoisone)
> return(out)
> }
>
> example result:
>
> > mix(tsus_same, 77)
> \$pid
> [1] 77
>
> \$one
> [1] 9
>
> \$two
> [1] 2
>
> \$diff
> [1] 8.577195
>
> \$noseqs
> [1] 11
>
> \$twogrp
> [1] "more"
>
> >
>
> I then use this function in another function that just runs this
> function through a lot of data:
>
>
> doset <- function(sameset) {
> pids = unique(c(sameset\$APID, sameset\$BPID))
> for (f in pids) {
> oputframe = data.frame(rbind(oputframe, mix(sameset, f)))
> }
> return(oputframe)
> }
>
> All values except \$twogrp are numbers. There are two possible values
> for \$twogrp, "one" and "more". the first one is more common and gets
> added to the data frame first. The result is that I cannot add the
> rows where this is "more" without getting
>
> 38: In `[<-.factor`(`*tmp*`, ri, value = "more") :
> invalid factor level, NAs generated
>
>
> Now, this is a pain in the neck. How can I merge these lists to the
> data frame and still have the value \$twogrp as a factor?
>
> Thanks, and I hope my code makes some sense!
>
> Karin
> --
> Karin Lagesen, PhD student
> karin.lagesen_at_medisin.uio.no
> http://folk.uio.no/karinlag
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

```--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help