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

John Kane jrkrideau at yahoo.ca
Thu Aug 3 14:42:03 CEST 2006


--- 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

Thanks



> On 8/2/06, John Kane <jrkrideau at yahoo.ca> wrote:
> >
> > Simple problem but I don't see the answer. I'm
> trying
> > to clean up some data
> > I have 120 columns in a data.frame.  I have one
> value
> > in a column named "blaw" that I want to change.
> How do
> > I find the coordinates. I can find the row by
> doing a
> > subset on the data.frame but how do I find out
> here
> > "blaw " is in columns without manually counting
> them
> > or converting names(Df) to a list and reading down
> the
> > list.
> >
> > Simple example
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 0)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > Df
> > subset(Df, bat >= 50)
> >
> > ----results
> > cat dog rat bat
> > 5   0   0   0  54
> >
> >
> > Thus I know that my target is in row 5 but how do
> I
> > figure out where 'bat' is?
> >
> > All I want to do is be able to say
> > Df[5,4] <- 100
> >
> > Is there some way to have function(bat) return the
> > column number: some kind of a colnum() function? 
> I
> > had thought that I had found somthing  in
> > library(gdata) matchcols but no luck.
> >
> > ______________________________________________
> > R-help at 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.
> >
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
>



More information about the R-help mailing list