Remove columns that contain a specific word

[亡魂溺海] 提交于 2020-02-03 09:56:24

问题


I have a data set that has 313 columns, ~52000 rows of information. I need to remove each column that contains the word "PERMISSIONS". I've tried grep and dplyr but I can't seem to get it to work.

I've read the file in,

testSet <- read.csv("/Users/.../data.csv")

Other examples show how to remove columns by name but I don't know how to handle wildcards. Not quite sure where to go from here.


回答1:


From what I could understand from the question, the OP has a data frame like this:

df <- read.table(text = '
           a b c d
           e f PERMISSIONS g
           h i j k
           PERMISSIONS l m n',
                 stringsAsFactors = F)

The goal is to remove every column that has any 'PERMISSIONS' entry. Assuming that there's no variability in 'PERMISSIONS', this code should work:

cols <- colSums(mapply('==', 'PERMISSIONS', df))
new.df <- df[,which(cols == 0)]



回答2:


If you want to just remove columns that are named PERMISSIONS then you can use the select function in the dplyr package.

df <- data.frame("PERMISSIONS" = c(1,2), "Col2" = c(1,4), "Col3" = c(1,2))

PERMISSIONS Col2 Col3
1    1    1
2    4    2

df_sub <- select(df, -contains("PERMISSIONS"))

Col2 Col3
1    1
4    2



回答3:


Try this,

New.testSet <- testSet[,!grepl("PERMISSIONS", colnames(testSet))]

EDIT: changed script as per comment.




回答4:


We can use grepl with ! negate,

New.testSet <- testSet[!grepl("PERMISSIONS",row.names(testSet)),
                         !grepl("PERMISSIONS", colnames(testSet))]



回答5:


It looks like these answers only do part of what you want. I think this is what you're looking for. There is probably a better way to write this though.

library(data.table)
df = data.frame("PERMISSIONS" = c(1,2), "Col2" = c("PERMISSIONS","A"), "Col3" = c(1,2))

  PERMISSIONS        Col2 Col3
1           1 PERMISSIONS    1
2           2           A    2

df = df[,!grepl("PERMISSIONS",colnames(df))]
setDT(df)
ind = df[, lapply(.SD, function(x) grepl("PERMISSIONS", x, perl=TRUE))] 
df[,which(colSums(ind) == 0), with = FALSE]

   Col3
1:    1
2:    2


来源:https://stackoverflow.com/questions/41815039/remove-columns-that-contain-a-specific-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!