how to remove correlated or duplicated variable or individuals in r
dataframe, r
Solution
This should do the trick:
dat <- mydf[-1]
cMat <- abs(cor(dat)) >= (1 - .Machine$double.eps^0.5)
whichKeep <- which(rowSums(lower.tri(cMat) * cMat) == 0)
cbind(mydf[1], mydf[whichKeep + 1])
Inv varA varB varC
1 1 1 1 1
2 2 1 0 0
3 3 1 1 0
4 4 0 0 0
5 5 1 1 1
6 6 1 1 1
Problem
I have the following type (however very large number of variables and ind) data: ``` mydf <- data.frame (Inv = 1:6, varA = c(1,1,1, 0,1,1), varB = c(1,0,1, 0, 1,1), varC = c(1,0,0, 0,1,1), varD = c(1,1,1, 0,1,1), varE = c(1,0,1, 0, 1,1), varF = c(1,1,1, 0, 1,1)) mydf Inv varA varB varC varD varE varF 1 1 1 1 1 1 1 1 2 2 1 0 0 1 0 1 3 3 1 1 0 1 1 1 4 4 0 0 0 0 0 0 5 5 1 1 1 1 1 1 6 6 1 1 1 1 1 1 ``` I want to do all one to one comparsion (both variables and individuals / subjects) and keep only one if they are duplicated and name of duplicated individuals / variables to different file as log: For example in above data: Among variables: ``` varA is exactly same as varD and varF - so I will just keep varA only in new data mydf$varA == mydf$varE [1] TRUE TRUE TRUE TRUE TRUE TRUE varB and varE has exactly same data - so I will just keep varB varC is unique ``` Among Inv( i.e. subjects): ``` 1, 5 and 6 are same -> so just keep 1 ``` Thus the resulting output file is ``` mydf <- data.frame (Inv = 1:4, varA = c(1,1,1, 0), varB = c(1,0,1, 0), varC = c(1,0,0, 0)) Inv varA varB varC 1 1 1 1 1 2 2 1 0 0 3 3 1 1 0 4 4 0 0 0 ``` I would be able to find duplication probably by correlation matrix: ``` cor(mydf[,-1]) varA varB varC varD varE varF varA 1.0000000 0.6324555 0.4472136 1.0000000 0.6324555 1.0000000 varB 0.6324555 1.0000000 0.7071068 0.6324555 1.0000000 0.6324555 varC 0.4472136 0.7071068 1.0000000 0.4472136 0.7071068 0.4472136 varD 1.0000000 0.6324555 0.4472136 1.0000000 0.6324555 1.0000000 varE 0.6324555 1.0000000 0.7071068 0.6324555 1.0000000 0.6324555 varF 1.0000000 0.6324555 0.4472136 1.0000000 0.6324555 1.0000000 ``` Ca we automate this process ?