[R] Updating selected variables in a data frame

Andrew C. Ward s195404 at student.uq.edu.au
Tue Mar 18 02:10:52 CET 2008


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



More information about the R-help mailing list