Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing values in a row

Tags:

dataframe

r

I am trying to compare values on data frame rows, and removing all the ones that match, with this

dat[!dat[1]==dat[2]]

where

> dat

returns

n1  n2
n1  n4
n4  n5
n1  n3
n4  n4

So i want it to compare the values and delete the last row, since both columns have the same data. But when i use the above code, it tells me

Error in Ops.factor(left, right) : level sets of factors are different

the str(dat) reads

'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
like image 658
Jose187 Avatar asked Jun 16 '12 13:06

Jose187


1 Answers

I suspect in the creation of your data, you inadvertently and implicitly converted your columns to factors. This possibly happened when you read the data from source, e.g. when using read.csv or read.table. This example illustrates it:

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 remedy is to pass the argument stringsAsFactors=FALSE 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've missed a comma):

dat[!dat[1]==dat[2], ]
  V1 V2
1 n1 n2
2 n1 n4
3 n4 n5
4 n1 n3
like image 59
Andrie Avatar answered Sep 20 '22 10:09

Andrie