# [R] Addition operation based on specific columns and rows of twodata frames

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Sat Oct 13 00:29:42 CEST 2007

```My guess is that there's  an easier way but this gives what you want.

newY.df<-aggregate(Y.df\$Counts, list(Y.df[,1],Y.df[,2]), FUN=sum)
names(newY.df)<-names(X.df)

temp.df<-merge(newY.df, X.df,
by=intersect(names(X.df),names(newY.df)),all=TRUE)

almost.df<-aggregate(temp.df\$Counts, list(temp.df[,1],temp.df[,2]),
FUN=sum)

temp<-order(almost.df\$Group.1)
final.df<-almost.df[temp,]
names(final.df)<-names(X.df)

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Luke Neraas
Sent: Friday, October 12, 2007 5:15 PM
To: r-help
Subject: [R] Addition operation based on specific columns and rows of
twodata frames

#Hello,

# I have a question about the addition of values in specific columns and
rows of a Data frame.
# Below I have created two data frames, X.df and "Y.df".

## creation of X.df data frame

X<- matrix(0,16,3)
X.df<-data.frame(X)
X.df[,1] <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
X.df[,2] <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
names(X.df)<-"L(A)a(i)"
names(X.df)<-"L(B)a(j)"
names(X.df)<-"Counts"

X.df

## creation of Y.df data frame

Y<- matrix(0,7,3)
Y.df<-data.frame(Y)
Y.df[,1]<-c(1,3,4,2,2,1,2)
Y.df[,2]<-c(2,4,2,4,1,2,3)
Y.df[,3]<-c(1,2,1,0.5,1,1,1)
names(Y.df)<-"L(A)a(i)"
names(Y.df)<-"L(B)a(j)"
names(Y.df)<-"Counts"

Y.df

# I would like to add the value in the "Counts" column of Y.df to the
"Counts" Column of X.df, based on matching # the row value from the
first two columns of each data.frame.  I have tried the "merge" function
but that # doesn't give me the addition operation.

# here is what i would like the resulting operation to end up with in
the X.df data frame

X.df\$Counts <- c(0,2,0,0,1,0,1,0.5,0,0,0,2,0,1,0,0)

X.df

# Any help or ideas would be greatly appreciated

# Luke Neraas

# lukasneraas.r at gmail.com

# School of Fisheries and Ocean Sciences # 11120 Glacier Highway # UAF
Fisheries Division # Juneau, AK 99801

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
{"ft_lang_label":"__label__en","ft_lang_prob":0.71298164,"math_prob":0.95400083,"size":2623,"snap":"2021-31-2021-39","text_gpt3_token_len":857,"char_repetition_ratio":0.13631158,"word_repetition_ratio":0.05466238,"special_character_ratio":0.37133053,"punctuation_ratio":0.25034964,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99411196,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-28T02:02:56Z\",\"WARC-Record-ID\":\"<urn:uuid:6ff2c7cc-04e9-4767-9653-2a8d0a20bf21>\",\"Content-Length\":\"5914\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:280dc367-b96f-408f-b554-50b16114445c>\",\"WARC-Concurrent-To\":\"<urn:uuid:b09f333a-dbb2-4924-b727-e925fdb48844>\",\"WARC-IP-Address\":\"129.132.119.195\",\"WARC-Target-URI\":\"https://stat.ethz.ch/pipermail/r-help/2007-October/143141.html\",\"WARC-Payload-Digest\":\"sha1:SPNZNOIH77VV4S43ZJ6ZKFHGP2YFERGL\",\"WARC-Block-Digest\":\"sha1:4TUGPY7IIZU36V2QXFKQ7KZCOTJNYH6Y\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153515.0_warc_CC-MAIN-20210727233849-20210728023849-00204.warc.gz\"}"}