I have two dataframes:
df1:
WAV UV VIOLET BLUE
sD1 10.8 10.1 23.5
sA4 6.2 8.2 19.9
sA1 8.3 11.7 28.6
sC2 7.9 8.2 31.0
sC3 10.7 9.5 18.1
df2:
ID UV VIOLET BLUE
D1 7.9 10.1 19.3
D2 7.0 9.2 15.9
D3 21.4 20.7 27.4
D4 10.3 8.9 20.9
D5 21.7 16.5 21.3
I want to find the difference between the sum of the columns of D1 in df2 and the columns of each row in df1 and produce this output in a new dataframe. Then, this needs to be repeated for D2 of df2 with every row of df1 and so on. Each new difference between the sums for each row should be a separate entry of the new dataframe and each list of the differences row of df2 should be a new row in the output. So the output should look like this:
D1 sum(D1)-sum(sD1) sum(D1)-sum(sA4) sum(D1)-sum(sA1) sum(D1-sC2) sum(D1)-sum(sC3)
D2 sum(D2)-sum(sD1) sum(D2)-sum(sA4) sum(D2)-sum(sA1) sum(D2-sC2) sum(D2)-sum(sC3)
D3 sum(D3)-sum(sD1) sum(D3)-sum(sA4) sum(D3)-sum(sA1) sum(D3-sC2) sum(D3)-sum(sC3)
D4 sum(D4)-sum(sD1) sum(D4)-sum(sA4) sum(D4)-sum(sA1) sum(D4-sC2) sum(D4)-sum(sC3)
D5 sum(D5)-sum(sD1) sum(D5)-sum(sA4) sum(D5)-sum(sA1) sum(D5-sC2) sum(D5)-sum(sC3)
I'm open to any suggestions.
Here are three ways, two of which have already been mentioned by @Onyambu in comment. Of these, the outer
option seems to be the fastest.
outer(rowSums(df1[,-1]), rowSums(df2[,-1]), "-")
or
sapply(rowSums(df1[-1]),"-",rowSums(df2[-1]))
or
sapply(rowSums(df1[,-1]), function(x) x - rowSums(df2[,-1]))
So for instance, you can do:
df <- data.frame(outer(rowSums(df1[,-1]), rowSums(df2[,-1]), "-"))
colnames(df) <- df1$WAV
rownames(df) <- df2$ID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.