Removing Column from Dataframe that contains a string in R -
i'm trying create shiny app allows people input data. make data usable i'm trying remove column has instance of string in dataframe df. task simple in python in r has become quite tough.
my first try
df[ ,colsums(apply(df, margin=c(1,2),fun=is.character))==0]
the problem apply implicitly calls as.matrix (as understand it) make dataframe mixed values is.character drops every column if column has string in it!
i tried few loops go through each cell , try drop column has been failing well.
any or pointers superbly helpful!
you cannot have column of mixed types (at least not in r). column can have numeric, character or factor values, not values different types. have check if columns character or not, , 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
Comments
Post a Comment