--- Peter Dalgaard <[EMAIL PROTECTED]> wrote: > John Kane <[EMAIL PROTECTED]> writes: > > > --- jim holtman <[EMAIL PROTECTED]> 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. ______________________________________________ [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.
