0

I have a column called Region in a data frame which is of character type. It has certain junk values as below which I want to remove:

"#VALUE!","10.1","10.2","138","145","161"

But when I try to remove using things like subset they don't get removed as follows:

subset(pro_202_data,Region != c("#VALUE!","10.1","10.2","138","145","161"))

I have tried using only != but that also doesn't work.

Please help.

Wil
  • 3,076
  • 2
  • 12
  • 31

2 Answers2

0

Does this answer, I've created a dataframe with what you've provided and tried to filter out first two rows, you can try similarly for your entire dataframe.

> pro_202_data
   Region
1 #VALUE!
2    10.1
3    10.2
4     138
5     145
6     161
> subset(pro_202_data, !(Region %in% c("#VALUE!","10.1")))
  Region
3   10.2
4    138
5    145
6    161
> 
Karthik S
  • 11,348
  • 2
  • 11
  • 25
0

You can subset like this:

Single vector:

x <- c("#VALUE!","10.1","10.2","138","145","161")
x[!x=="#VALUE!"]
[1] "10.1" "10.2" "138"  "145"  "161"

Dataframe:

df <- data.frame(
  Region = c("#VALUE!","10.1","10.2","138","145","161"), stringsAsFactors = F
)

df[!df$Region=="#VALUE!",]
[1] "10.1" "10.2" "138"  "145"  "161"

Note the addition of the ,to select all columns of the dataframe.

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34