[R] problems with data frames, factors and lists

From: Karin Lagesen <karinlag_at_studmed.uio.no>
Date: Wed, 21 May 2008 17:33:07 +0200

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