r - comparing values in a row -


i trying compare values on data frame rows, , removing ones match, this

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

where

> dat 

returns

n1  n2 n1  n4 n4  n5 n1  n3 n4  n4 

so want compare values , delete last row, since both columns have same data. when use above code, tells me

error in ops.factor(left, right) : level sets of factors 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 

i suspect in creation of data, inadvertently , implicitly converted columns factors. possibly happened when read data source, e.g. when using read.csv or read.table. 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 pass argument stringsasfactors=false 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 code works (except suspect you've missed comma):

dat[!dat[1]==dat[2], ]   v1 v2 1 n1 n2 2 n1 n4 3 n4 n5 4 n1 n3 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -