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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Aug 3 15:03:01 CEST 2006


John Kane <jrkrideau at yahoo.ca> writes:

> --- jim holtman <jholtman at 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. 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


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list