Reputation: 283
Example:
df <- data.frame(A=1:5, B=seq(0,10,2), C=seq(0,15,3))
df
A B C
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
What I want is:
A B C (A-B) (A-C) (B-C)
1 2 3 -1 -2 -1
2 4 6 -2 -4 -2
3 6 9 -3 -6 -3
4 8 12 -4 -8 -4
5 10 15 -5 -10 -5
This is a sample. In my problem I have over 100 columns
Any suggestions on how to do this in R?
Upvotes: 5
Views: 1327
Reputation: 23109
with mapply
:
# the data: create the input data frame with mapply for fun
df <- as.data.frame(mapply(function(i,j) seq(0, 5*i, j), 1:5, 1:5))
names(df) <- LETTERS[1:5]
df
# A B C D E
#1 0 0 0 0 0
#2 1 2 3 4 5
#3 2 4 6 8 10
#4 3 6 9 12 15
#5 4 8 12 16 20
#6 5 10 15 20 25
# now use mapply to solve our problem
pairs <- combn(seq_len(ncol(df)), 2) # find all possible pairs
mapply(function(i,j)df[,i]-df[,j], pairs[1,], pairs[2,])
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 0 0 0 0 0 0 0 0 0 0
#[2,] -1 -2 -3 -4 -1 -2 -3 -1 -2 -1
#[3,] -2 -4 -6 -8 -2 -4 -6 -2 -4 -2
#[4,] -3 -6 -9 -12 -3 -6 -9 -3 -6 -3
#[5,] -4 -8 -12 -16 -4 -8 -12 -4 -8 -4
#[6,] -5 -10 -15 -20 -5 -10 -15 -5 -10 -5
Upvotes: 1
Reputation: 887501
We can use the FUN
argument in combn
combn(seq_along(df), 2, FUN = function(x) df[,x[1]]- df[,x[2]])
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] -1 -2 0 -2 -1 1 -1 2 0 -2
#[2,] -2 -4 -4 -7 -2 -2 -5 0 -3 -3
#[3,] -3 -6 -8 -12 -3 -5 -9 -2 -6 -4
#[4,] -4 -8 -12 -17 -4 -8 -13 -4 -9 -5
#[5,] -5 -10 -16 -22 -5 -11 -17 -6 -12 -6
Also, combn
takes the data.frame
as argument, so simply
combn(df, 2, FUN = function(x) x[,1]-x[,2])
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] -1 -2 0 -2 -1 1 -1 2 0 -2
#[2,] -2 -4 -4 -7 -2 -2 -5 0 -3 -3
#[3,] -3 -6 -8 -12 -3 -5 -9 -2 -6 -4
#[4,] -4 -8 -12 -17 -4 -8 -13 -4 -9 -5
#[5,] -5 -10 -16 -22 -5 -11 -17 -6 -12 -6
df <- data.frame(A=1:5, B=seq(2,10,2), C=seq(3,15,3), d=seq(1,25,5), e=seq(3,31,6))
Upvotes: 5
Reputation: 8413
using combn
that generates all combinations of 2
(specified in the call) colnames
m = data.frame(combn(colnames(df),2), stringsAsFactors = F)
m
# X1 X2 X3
#1 A A B
#2 B C C
df1 = data.frame(lapply(m, function(x) df[[x[2]]] - df[[x[1]]]))
colnames(df1) = lapply(m, function(x) paste(x[2], x[1], sep="-"))
cbind(df,df1)
# A B C B-A C-A C-B
#1 1 0 0 -1 -1 0
#2 2 2 3 0 1 1
#3 3 4 6 1 3 2
#4 4 6 9 2 5 3
#5 5 8 12 3 7 4
Upvotes: 2
Reputation: 837
This worked for me:
col_diff<-function(df)
{
dif_fn<-function(obj){return(obj[[1]]-obj[[2]])}
tmp_fn<-function(inp_row){unlist(combn(inp_row,2,FUN=dif_fn,simplify = FALSE))}
intres<-t(apply(df,1,tmp_fn))
colnames(intres)<-unlist(combn(colnames(df),2,simplify = FALSE,FUN=function(obj){paste(obj[[1]],obj[[2]],sep="-")}))
temp<-cbind(df, intres)
return(temp)
}
Applied to your input:
df <- data.frame(A=1:5, B=seq(2,10,2), C=seq(3,15,3))
df
A B C
1 1 2 3
2 2 4 6
3 3 6 9
4 4 8 12
5 5 10 15
col_diff(df)
A B C A-B A-C B-C
1 1 2 3 -1 -2 -1
2 2 4 6 -2 -4 -2
3 3 6 9 -3 -6 -3
4 4 8 12 -4 -8 -4
5 5 10 15 -5 -10 -5
Upvotes: 2
Reputation: 9313
Here you go
df <- data.frame(A=1:5, B=seq(2,10,2), C=seq(3,15,3), d=seq(1,25,5), e=seq(3,31,6))
> df
A B C d e
1 1 2 3 1 3
2 2 4 6 6 9
3 3 6 9 11 15
4 4 8 12 16 21
5 5 10 15 21 27
z = combn(1:ncol(df),2)
> z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 2 2 2 3 3 4
[2,] 2 3 4 5 3 4 5 4 5 5
y = apply(z,2,function(x){
df[,x[1]]-df[,x[2]]
})
Result:
> y
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -1 -2 0 -2 -1 1 -1 2 0 -2
[2,] -2 -4 -4 -7 -2 -2 -5 0 -3 -3
[3,] -3 -6 -8 -12 -3 -5 -9 -2 -6 -4
[4,] -4 -8 -12 -17 -4 -8 -13 -4 -9 -5
[5,] -5 -10 -16 -22 -5 -11 -17 -6 -12 -6
The matrix z tells you which pair of columns were substracted
You do realize that if df has 100 columns, all the combinations add up to 4950.
Upvotes: 2