R如果列值等于列名,如何删除列

时间:2022-11-05 13:08:36

I have a data set that contains a bunch of columns where the column values(all values of that column) is exactly the same as that column name. How can I delete that column. I cannot delete the columns one by one as there are over 700 variables. Thanks!

我有一个包含一堆列的数据集,其中列值(该列的所有值)与该列名称完全相同。如何删除该列。我不能一个一个地删除列,因为有超过700个变量。谢谢!

3 个解决方案

#1


3  

Here is an example of how you could do it:

以下是如何执行此操作的示例:

data = data.frame(x1 = rep(1,10), x2 = seq(1,20,by = 2),
                  x3 = rep("x3",10), x4 = 1:10, x5 = rep("x5",10))

col_rm = which(sapply(1:ncol(data), function(x) all(data[,x] == colnames(data)[x])))
data = data[,-col_rm]

basically what the code does it that finds if all the values of the column x are equal to the name of the column and finds all the columns that fulfill that condition and then i just remove them with -.

基本上代码所做的是,如果列x的所有值都等于列的名称,并找到满足该条件的所有列,那么我只需用 - 删除它们。

#2


1  

Does this help?

这有帮助吗?

data = data.frame(name1=rep("name1",5), name2=rep("name2",5), name3=rep("name3",5), name4=rep("name4",5)) #Some test data

ColsToRemove <- names(data)[which(sapply(data[1,], function(x){x %in% names(data)}))] #Finds where the column name is the same as the first entry and marks it for deletion

cleanData = data[ , !(names(data) %in% ColsToRemove)] #This deletes the columns named

head(data)
head(cleanData)

#3


0  

You can use sweep to determine all the values that are not equal to the colnames

您可以使用扫描来确定所有不等于组合名的值

data[colSums(sweep(data,2,colnames(data),"!="))>0]

#1


3  

Here is an example of how you could do it:

以下是如何执行此操作的示例:

data = data.frame(x1 = rep(1,10), x2 = seq(1,20,by = 2),
                  x3 = rep("x3",10), x4 = 1:10, x5 = rep("x5",10))

col_rm = which(sapply(1:ncol(data), function(x) all(data[,x] == colnames(data)[x])))
data = data[,-col_rm]

basically what the code does it that finds if all the values of the column x are equal to the name of the column and finds all the columns that fulfill that condition and then i just remove them with -.

基本上代码所做的是,如果列x的所有值都等于列的名称,并找到满足该条件的所有列,那么我只需用 - 删除它们。

#2


1  

Does this help?

这有帮助吗?

data = data.frame(name1=rep("name1",5), name2=rep("name2",5), name3=rep("name3",5), name4=rep("name4",5)) #Some test data

ColsToRemove <- names(data)[which(sapply(data[1,], function(x){x %in% names(data)}))] #Finds where the column name is the same as the first entry and marks it for deletion

cleanData = data[ , !(names(data) %in% ColsToRemove)] #This deletes the columns named

head(data)
head(cleanData)

#3


0  

You can use sweep to determine all the values that are not equal to the colnames

您可以使用扫描来确定所有不等于组合名的值

data[colSums(sweep(data,2,colnames(data),"!="))>0]