Re: [R] Vector DF [a, b] to replace values :was Finding the position of a variable in a data.frame

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Fri 04 Aug 2006 - 02:09:56 EST

> John Kane <jrkrideau@yahoo.ca> writes:
>
> > --- jim holtman <jholtman@gmail.com> wrote:
> >
> > > ?which
> > >
> > > > which(Df >= 50, arr.ind=T)
> > > row col
> > > 5 5 4
> >
> > This works very nicely as has some other
> suggestions
> > on how to replace a value. Assuming that I have
> more
> > than one correction to make where Df >= 50, can I
> use
> > vectors in the Df[] to do this.
> >
> > My attempt shows that I can use vectors but it
> appears
> > thatthere is something wrong with my logic
> >
> > Eample
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 51)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > post <- which(Df >= 50, arr.ind=T) # find values
> .=
> > 50
> > post
> > correction <- c(77, 88) # new correct values
> > row <- post[ ,1] # vector for row number
> > col <- post[ ,2] # vector for column number
> >
> > Df[row,col] <- correction
> > Df
> >
> > -------result---------
> > cat dog rat bat
> > 1 3 3 5 12
> > 2 5 5 5 42
> > 3 6 3 4 45
> > 4 8 6 9 32
> > 5 0 0 88 88
> >
> > I am replacing both instances with 88 which is not
> > correct
>
> You're being bitten by two issues here: One is that
> Df[row,col] does
> not vectorize in parallel in the two indices:
> Df[1:2,1:2] has *four*
> elements, not two.

It took a moment to see it but it's rather obvious now. I'm still fuzzy on the differences in behaviour between a data frame and a matrix.

> Another is that data frames are
> not matrices: you
> can have different types of values in different
> columns, so you cannot
> expect to pick an arbitrary set of elements and
> assign a vector into
> it (well, it doesn't work, anyway).
>
> This sort of stuff works much easier with matrices,
> where we also have
> the wonderful feature of indexing with a matrix:
>
> > M <- cbind(cat, dog, rat, bat)
> > M[post]<- correction
> > M
> cat dog rat bat
> [1,] 3 3 5 12
> [2,] 5 5 5 42
> [3,] 6 3 4 45
> [4,] 8 6 9 32
> [5,] 0 0 77 88

Which of course I, stupidly thought that I was doing in the data frame.

Thank you very much.



R-help@stat.math.ethz.ch 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. Received on Fri Aug 04 02:24:05 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 04 Aug 2006 - 04:18:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.