简体   繁体   中英

Removing Column from Dataframe that contains a string in R

I'm trying to create a shiny app that allows people to input data. To make the data usable I'm trying to remove any column that has an instance of a string in a dataframe df. This task was really simple in python but in R has become quite tough.

My first try was

df[ ,colSums(apply(df, MARGIN=c(1,2),FUN=is.character))==0]

The problem is apply implicitly calls as.matrix which (as I understand it) will make a dataframe with mixed values so is.character drops every column if any column has a string in it!

I tried a few loops to go through each cell and try to drop a column but that has been failing as well.

Any help or pointers would be superbly helpful!

You cannot have a column of mixed types (at least not in R). So a column can have numeric, character or factor values, but not values from different types. So you only have to check if the columns are character or not, and not every single value. Try:

df <- data.frame(c1 = 1:10, c2 = letters[1:10], c3 = sample(10, 10), c4 = letters[sample(10,10)], stringsAsFactors = FALSE)
df[!sapply(df, is.character)]
   c1 c3
1   1  4
2   2 10
3   3  5
4   4  6
5   5  7
6   6  3
7   7  9
8   8  8
9   9  2
10 10  1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM