Hello, I would like to be able to update an existing column in a dataframe, like this... data$score[data$type=="1" & data$year=="2001"]<-data$score * 0.111 data$score[data$type=="1" & data$year=="2002"]<-data$score * 0.222 data$score[data$type=="1" & data$year=="2003"]<-data$score * 0.333 ...but, if possible, using simpler code. I've got several dozen lines of code like this (type 2, type3, etc. for the same years) so it would be great if I could reduce each set of three lines of code to one line Any help much appreciated, thanks! Mark
How to update a column in a dataframe, more simply...
2 messages · Mark, jim holtman
First of all, what you have will not work since you also have to
subset the RHS of the equation:
data$score[data$type=="1" &
data$year=="2001"]<-data$score[data$type=="1" & data$year=="2001"] *
0.111
Another way is to construct a matrix of the values you want to search
for and change: (not tested)
change <- rbind(c(1, 2001, 0.111),
c(1, 2002, 0.222),
c(1, 2003, 0.333),
c(2, 2001, 1.111),
.....)
for (i in seq(nrow(change))){
select <- data$type == change[i,1] & data$year == change[i,2]
data$score[select] <- data$score[select] * change[i,3]
}
On Fri, Sep 26, 2008 at 6:35 PM, Mark Na <mtb954 at gmail.com> wrote:
Hello, I would like to be able to update an existing column in a dataframe, like this... data$score[data$type=="1" & data$year=="2001"]<-data$score * 0.111 data$score[data$type=="1" & data$year=="2002"]<-data$score * 0.222 data$score[data$type=="1" & data$year=="2003"]<-data$score * 0.333 ...but, if possible, using simpler code. I've got several dozen lines of code like this (type 2, type3, etc. for the same years) so it would be great if I could reduce each set of three lines of code to one line Any help much appreciated, thanks! Mark
______________________________________________ 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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?