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
Updating selected variables in a data frame
2 messages · Andrew C. Ward, Gabor Grothendieck
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.