R 查找完全相关/冗余的数字列和字符列
我有一个有几百列的数据集。它包含邮件列表数据,其中几个列似乎彼此完全重复,但形式不同 例如:R 查找完全相关/冗余的数字列和字符列,r,correlation,R,Correlation,我有一个有几百列的数据集。它包含邮件列表数据,其中几个列似乎彼此完全重复,但形式不同 例如: rowNum StateCode StateName StateAbbreviation 1 01 UTAH UT 2 01 UTAH UT 3 03 TEXAS
rowNum StateCode StateName StateAbbreviation
1 01 UTAH UT
2 01 UTAH UT
3 03 TEXAS TX
4 03 TEXAS TX
5 03 TEXAS TX
6 44 OHIO OH
7 44 OHIO OH
8 44 OHIO OH
... ... ... ...
我想删除重叠的数据,如果可能的话只保留数字列,这样只有一列包含相同的信息。因此,上述示例将变成:
rowNum StateCode
1 01
2 01
3 03
4 03
5 03
6 44
7 44
8 44
... ...
我尝试过使用cor()
,但这只适用于数值变量。我尝试了插入符号::nearZeroVar(),但这只适用于列本身中的
有没有人对查找包含非数字数据的完全相关列有什么建议
谢谢。
dat这里有一个有趣且快速的解决方案。它首先将data.frame转换为适当结构的整数类矩阵,然后使用cor()
标识冗余列
dat <- read.table(text="rowNum StateCode StateName
1 01 UTAH
2 01 UTAH
3 03 TEXAS
4 03 TEXAS
5 03 TEXAS
6 44 OHIO
7 44 OHIO
8 44 OHIO", header=TRUE)
dat [!duplicated(dat[, 2:3]), ]
#------------
rowNum StateCode StateName
1 1 1 UTAH
3 3 3 TEXAS
6 6 44 OHIO
## Read in the data
df <- read.table(text="rowNum StateCode StateName StateAbbreviation
1 01 UTAH UT
2 01 UTAH UT
3 03 TEXAS TX
4 03 TEXAS TX
5 03 TEXAS TX
6 44 OHIO OH
7 44 OHIO OH
8 44 OHIO OH", header=TRUE)
## Convert data.frame to a matrix with a convenient structure
## (have a look at m to see where this is headed)
l <- lapply(df, function(X) as.numeric(factor(X, levels=unique(X))))
m <- as.matrix(data.frame(l))
## Identify pairs of perfectly correlated columns
M <- (cor(m,m)==1)
M[lower.tri(M, diag=TRUE)] <- FALSE
## Extract the names of the redundant columns
colnames(M)[colSums(M)>0]
[1] "StateName" "StateAbbreviation"
##读入数据
df这会起作用吗?我的想法是,如果调用表(col1,col2)
,
如果列重复,则表中的任何列将只有一个非零值,例如:
OHIO TEXAS UTAH
1 0 0 2
3 0 3 0
44 3 0 0
比如说:
dup.cols <- read.table(text='rowNum StateCode StateName StateAbbreviation
1 01 UTAH UT
2 01 UTAH UT
3 03 TEXAS TX
4 03 TEXAS TX
5 03 TEXAS TX
6 44 OHIO OH
7 44 OHIO OH
8 44 OHIO OH', header=T)
library(plyr)
combs <- combn(ncol(dup.cols), 2)
adply(combs, 2, function(x) {
t <- table(dup.cols[ ,x[1]], dup.cols[ , x[2]])
if (all(aaply(t1, 2, function(x) {sum(x != 0) == 1}))) {
paste("Column numbers ", x[1], x[2], "are duplicates")
}
})
dup.cols这应该会为您返回一个映射,告诉您哪些变量彼此匹配
check.dup <- expand.grid(names(dat),names(dat)) #find all variable pairs
check.dup[check.dup$Var1 != check.dup$Var2,] #take out self-reference
check.dup$id <- mapply(function(x,y) {
x <- as.character(x); y <- as.character(y)
#if number of levels is different, discard; keep the number for later
if ((n <- length(unique(dat[,x]))) != length(unique(dat[,y]))) {
return(FALSE)
}
#subset just the variables in question to get pairs
d <- dat[,c(x,y)]
#find unique pairs
d <- unique(d)
#if number of unique pairs is the number of levels from before,
#then the pairings are one-to-one
if( nrow(d) == n ) {
return(TRUE)
} else return(FALSE)
},
check.dup$Var1,
check.dup$Var2
)
check.dup这个问题问的是重复的列,而不是行。@马吕斯:如果你是-1票的来源,让我问你,在OP将问题更改为其他问题后,你是否认为对答案进行否决是合理的?当我发布这个答案时,没有“StateAbstration”栏,问题中也没有“正确答案”的例子。我不担心我的总分,但我认为当问题发生变化时,投反对票是很糟糕的。我同意——在这里投反对票没有任何用处,只是让这里变得不那么友好。对不起,我以为这是一个基于对问题的粗略阅读(“查找重复项!”)的回答。我没有意识到它是预先编辑的。只是编辑了我的答案以简化它的方法。它现在使用了cor()
,我当然应该从您的问题中开始学习。谢谢你提出这么酷的问题。@JoshO'Brien:很好用。非常感谢你。