Re: [R] Updating selected variables in a data frame

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 17 Mar 2008 22:36:40 -0400

Try this. It uses the fact that 2 column matrices can be used as subscripts (as described in the Matrices and arrays section of ?Extract):

with(recodes, replace(as.matrix(main), cbind(id, qst + 1), value))

It returns a matrix so use as.data.frame on that to turn it back to a data frame, if need be.

On Mon, Mar 17, 2008 at 9:10 PM, Andrew C. Ward <s195404_at_student.uq.edu.au> wrote:
> Dear list,
>
> I'd like to update certain rows/columns in a data
> frame with new values. The application is that
> survey respondents may give an answer of "Other"
> to a categorical question and then provide some
> text describing what they mean. This text is then
> reviewed and placed into a category. These edits
> or recodes then need to be merged back into the
> main dataset.
>
> A small example follows.
>
> # Generate a main data set (10 people, 2 questions)
> main <- data.frame(id=1:10, q1=sample(1:5, size=10,
> replace=TRUE), q2=sample(1:5,
> size=10, replace=TRUE))
> # Generate a corresponding recodes dataset
> recodes <- reshape(main, idvar="id", direction="long",
> varying=list(2:3), v.names="value",
> timevar="qst")
> recodes <- recodes[sample(seq(nrow(recodes)), 10,
> replace=FALSE),]
> recodes$value <- recodes$value + sign(runif(1)-0.5)
>
> I could easily use a for() loop to update the main
> dataset. The recodes dataset can get quite large,
> however, so I would rather avoid this.
>
> The question:
> Is there some efficient way, using apply() perhaps,
> that I can update the main dataset with the new
> values from the recodes dataset?
>
> Thanks very much for your advice!
>
> Regards,
>
> Andrew C. Ward
>
> CAPE Centre
> Department of Chemical Engineering
> The University of Queensland
> Brisbane Qld 4072 Australia
>
> ______________________________________________
> R-help_at_r-project.org 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.
>



R-help_at_r-project.org 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 Tue 18 Mar 2008 - 02:41:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Mar 2008 - 03:30:23 GMT.

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

list of date sections of archive