[R] Updating selected variables in a data frame

From: Andrew C. Ward <s195404_at_student.uq.edu.au>
Date: Tue, 18 Mar 2008 11:10:52 +1000

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",
recodes <- recodes[sample(seq(nrow(recodes)), 10,

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!


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. Received on Tue 18 Mar 2008 - 01:21:35 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 - 07: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