Thursday, 12 September 2013

data frame to structured list

data frame to structured list

This is an interesting question that I can't seem to find the answer to.
Let me just jump right in. I need to take a data frame created from:
TPA <- ddply(MT,~plot,summarise,TPA=length(unique(tree.number))*50)
> TPA
plot TPA
1 10A 700
2 10B 1000
3 1A 900
4 1B 950
5 2A 950
6 2B 650
7 3A 650
8 3B 1350
9 4A 1450
10 4B 1350
11 5A 850
12 5B 1100
13 6A 1050
14 6B 550
15 7A 850
16 7B 800
17 8A 2450
18 8B 950
19 9A 1150
20 9B 1000
and convert this to:
y <- list(one=c(900,950), two=c(950,650), three=c(650,1350),
four=c(1450,1350), five=c(850,1100), six=c(1050,550),
seven=c(850,800), eight=c(2450,950), nine=c(1150,1000), ten=c(700,1000))
> y
$one
[1] 900 950
$two
[1] 950 650
$three
[1] 650 1350
$four
[1] 1450 1350
$five
[1] 850 1100
$six
[1] 1050 550
$seven
[1] 850 800
$eight
[1] 2450 950
$nine
[1] 1150 1000
$ten
[1] 700 1000
Notice that 1A in the data frame corresponds to "one" in the list. I know
how to go the other way from a list to a data frame, but I can't figure
out how to go from a data frame to a list. I have a function that requires
a list to feed into the function. For completeness, here is the part that
uses the list:
yi.bar <- unlist(lapply(y,mean))
s2i <- unlist(lapply(y,var))
Any suggestions?
Thank you!

No comments:

Post a Comment