This should get you close:
> x <- read.table(textConnection("st vc y
+ A Z .2
+ B Z .4
+ B Y .3
+ C Y .1
+ C X .8"), header=TRUE)
> closeAllConnections()
>
> x
st vc y
1 A Z 0.2
2 B Z 0.4
3 B Y 0.3
4 C Y 0.1
5 C X 0.8
> x.1 <- melt(x)
Using st, vc as id variables
> x.1
st vc variable value
1 A Z y 0.2
2 B Z y 0.4
3 B Y y 0.3
4 C Y y 0.1
5 C X y 0.8
> cast(x.1, st ~ vc)
st X Y Z
1 A NA NA 0.2
2 B NA 0.3 0.4
3 C 0.8 0.1 NA
>
On Mon, Apr 26, 2010 at 10:35 AM, Robin Jeffries <[email protected]> wrote:
> I do get the following error message:
>
> *Error in lookup.svc[i, j] <- svc[svc$st == unique(svc$st)[i] & svc$vc ==
> : *
> * replacement has length zero*
>
>
> I also thought it might be because of how R treats NA, but then I would
> expect the loop to stop at the place of error (i=1, j=2) and not continue
> to
> fill out all of column i.
>
> I've tried using %in%, but that seems to do a non-positional check for
> whether or not entries are *somewhere *in those vectors. I need to find the
> location of where the match occurs.
>
> My goal is to turn this:
>
> st vc y
> A Z .2
> B Z .4
> B Y .3
> C Y .1
> C X .8
>
> into a 2x2 table with entries 'y'
> vc
> Z Y X
> A .2 0 0
> st B .4 .3 0
> C 0 .1 .8
>
>
> Right now it's giving me
>
> vc
> Z Y X
> A .2 0.000 0.000
> st B 0 0 0
> C 0 0 0
>
> So it seems to finish out the row that it's currently on, but then won't
> continue to loop.
>
>
> -Robin
>
>
>
> On Sun, Apr 25, 2010 at 4:44 PM, Peter Alspach <
> [email protected]> wrote:
>
> > Tena koe Robin
> >
> > Do you get an error or warning?
> >
> > It may have something to do with how == treats NA:
> >
> > x <- 1:4
> > x[x == 1]
> > [1] 1
> > x <- c(1:4, NA)
> > x[x == 1]
> > [1] 1 NA
> > x[x %in% 1]
> > [1] 1
> >
> > If so, using %in% is one way to avoid the problem. However, I would
> > have thought you'd get an error message if this were the case.
> >
> > HTH .....
> >
> > Peter Alspach
> >
> > > -----Original Message-----
> > > From: [email protected] [mailto:r-help-boun...@r-
> > > project.org] On Behalf Of Robin Jeffries
> > > Sent: Monday, 26 April 2010 10:26 a.m.
> > > To: [email protected]
> > > Subject: [R] Obvious reason for not looping twice?
> > >
> > > Is there an obvious reason why this won't loop to i=2 and beyond?
> > > There are many combinations of *st* & *vc* that don't exist in svc.
> > For
> > > example, when s=1 there's only an entry at v=1. That's fine, the
> > entry
> > > can
> > > stay 0.
> > >
> > > lookup.svc <-
> > > array(0,dim=c(length(unique(svc$st)),length(unique(svc$vc))),
> > > dimnames=list(unique(svc$st), unique(svc$vc)))
> > >
> > > for (i in 1:length(unique(svc$st))) {
> > > for (j in 1:length(unique(svc$vc))){
> > > lookup.svc[i,j] <- svc[svc$st == unique(svc$st)[i] & svc$vc ==
> > > unique(svc$vc)[j], 4]
> > > }}
> > >
> > >
> > > Thanks,
> > > Robin
> > >
> > > ~~~~~~~~~~~~~~~~~~~
> > > -Robin Jeffries
> > > Dr.P.H. Candidate
> > > UCLA School of Public Health
> > > [email protected]
> > > 530-624-0428
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [email protected] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-<http://www.r-project.org/posting->
> > > guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> 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 that you are trying to solve?
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.