I suspect that when you created your data, you inadvertently and implicitly converted your columns into factors. This is possible when you are reading data from a source, for example. when using read.csv
or read.table
. This example illustrates this:
dat <- read.table(text=" n1 n2 n1 n4 n4 n5 n1 n3 n4 n4") str(dat) 'data.frame': 5 obs. of 2 variables: $ V1: Factor w/ 2 levels "n1","n4": 1 1 2 1 2 $ V2: Factor w/ 4 levels "n2","n3","n4",..: 1 3 4 2 3
The tool consists in passing the stringsAsFactors=FALSE
argument to read.table()
:
dat <- read.table(text=" n1 n2 n1 n4 n4 n5 n1 n3 n4 n4", stringsAsFactors=FALSE) str(dat) 'data.frame': 5 obs. of 2 variables: $ V1: chr "n1" "n1" "n4" "n1" ... $ V2: chr "n2" "n4" "n5" "n3" ...
Then your code works (except that I suspect you missed the comma):
dat[!dat[1]==dat[2], ] V1 V2 1 n1 n2 2 n1 n4 3 n4 n5 4 n1 n3
source share