# eror code, not meaningful for factors

can somebody help me, I have tried several times but the results are still an error.

``````cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
cov\$mar[cov\$marstat==2]<-1
cov\$mar[cov\$marstat<=1]<-0
``````

cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
Warning message:
In Ops.factor(cov\$marstat, 1) : ‘>’ not meaningful for factors
cov\$mar[cov\$marstat==2]<-1
Warning message:
In `[<-.factor`(`*tmp*`, cov\$marstat == 2, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
cov\$mar[cov\$marstat<=1]<-0
Warning messages:
1: In `[<-.factor`(`*tmp*`, cov\$marstat <= 1, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
2: In Ops.factor(cov\$marstat, 1) : ‘<=’ not meaningful for factors

For example, it'll be much helpful to provide the `cov` dataset that you're using.

If the `marstat` column is a factor, and if it is unordered, then you can't really compare whether it's higher (or lower) than something or not. It really makes no sense, even if you denote the levels by numbers. Check below:

``````(y <- factor(x = 1:5, ordered = FALSE))
#> [1] 1 2 3 4 5
#> Levels: 1 2 3 4 5

y[1] > y[5]
#> Warning in Ops.factor(y[1], y[5]): '>' not meaningful for factors
#> [1] NA

(z <- factor(x = 1:5, ordered = TRUE))
#> [1] 1 2 3 4 5
#> Levels: 1 < 2 < 3 < 4 < 5

z[1] > z[5]
#> [1] FALSE
``````

Created on 2019-03-11 by the reprex package (v0.2.1)

thank you for replying my topic,
my data are
#> 1 2 3 4 5

the code are:

``````cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
cov\$mar[cov\$marstat==2]<-1
cov\$mar[cov\$marstat<=1]<-0
cov\$mar[cov\$marstat>=3]<-0
cov\$sep[cov\$marstat==3]<-1
cov\$sep[cov\$marstat<=2]<-0
cov\$sep[cov\$marstat>=4]<-0
cov\$div[cov\$marstat==4]<-1
cov\$div[cov\$marstat<=3]<-0
cov\$div[cov\$marstat>=5]<-0
cov\$wid[cov\$marstat==5]<-1
cov\$wid[cov\$marstat<=4]<-0
cov\$wid[cov\$marstat>=6]<-0
cov\$cohab[cov\$marstat==6]<-1
cov\$cohab[cov\$marstat<=5]<-0
``````

the result

cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
Warning message:
In Ops.factor(cov\$marstat, 1) : ‘>’ not meaningful for factors
cov\$mar[cov\$marstat==2]<-1
Warning message:
In `[<-.factor`(`*tmp*`, cov\$marstat == 2, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
cov\$mar[cov\$marstat<=1]<-0
Warning messages:
1: In `[<-.factor`(`*tmp*`, cov\$marstat <= 1, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
2: In Ops.factor(cov\$marstat, 1) : ‘<=’ not meaningful for factors
cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
Warning message:
In Ops.factor(cov\$marstat, 1) : ‘>’ not meaningful for factors
cov\$mar[cov\$marstat==2]<-1
Warning message:
In `[<-.factor`(`*tmp*`, cov\$marstat == 2, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
cov\$mar[cov\$marstat<=1]<-0
Warning messages:
1: In `[<-.factor`(`*tmp*`, cov\$marstat <= 1, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
2: In Ops.factor(cov\$marstat, 1) : ‘<=’ not meaningful for factors
cov\$mar[cov\$marstat>=3]<-0
Warning messages:
1: In `[<-.factor`(`*tmp*`, cov\$marstat >= 3, value = c(2L, 2L, 2L, :
invalid factor level, NA generated
2: In Ops.factor(cov\$marstat, 3) : ‘>=’ not meaningful for factors
cov\$sep[cov\$marstat==3]<-1
cov\$sep[cov\$marstat<=2]<-0
Warning message:
In Ops.factor(cov\$marstat, 2) : ‘<=’ not meaningful for factors
cov\$sep[cov\$marstat>=4]<-0
Warning message:
In Ops.factor(cov\$marstat, 4) : ‘>=’ not meaningful for factors
cov\$div[cov\$marstat==4]<-1
cov\$div[cov\$marstat<=3]<-0
Warning message:
In Ops.factor(cov\$marstat, 3) : ‘<=’ not meaningful for factors
cov\$div[cov\$marstat>=5]<-0
Warning message:
In Ops.factor(cov\$marstat, 5) : ‘>=’ not meaningful for factors
cov\$wid[cov\$marstat==5]<-1
cov\$wid[cov\$marstat<=4]<-0
Warning message:
In Ops.factor(cov\$marstat, 4) : ‘<=’ not meaningful for factors
cov\$wid[cov\$marstat>=6]<-0
Warning message:
In Ops.factor(cov\$marstat, 6) : ‘>=’ not meaningful for factors
cov\$cohab[cov\$marstat==6]<-1
cov\$cohab[cov\$marstat<=5]<-0
Warning message:
In Ops.factor(cov\$marstat, 5) : ‘<=’ not meaningful for factors

Letting aside the issue of the validity of your approach , you can achieve what you want by converting your factor variable to numeric beforehand.

``````cov <- data.frame(marstat = c("1", "2", "3", "4", "5"))
class(cov\$marstat)
#> [1] "factor"

cov\$marstat <- as.numeric(cov\$marstat)

cov\$nmar[cov\$marstat==1]<-1
cov\$nmar[cov\$marstat>1]<-0
cov\$mar[cov\$marstat==2]<-1
cov\$mar[cov\$marstat<=1]<-0
cov\$mar[cov\$marstat>=3]<-0
cov\$sep[cov\$marstat==3]<-1
cov\$sep[cov\$marstat<=2]<-0
cov\$sep[cov\$marstat>=4]<-0
cov\$div[cov\$marstat==4]<-1
cov\$div[cov\$marstat<=3]<-0
cov\$div[cov\$marstat>=5]<-0
cov\$wid[cov\$marstat==5]<-1
cov\$wid[cov\$marstat<=4]<-0
cov\$wid[cov\$marstat>=6]<-0
cov\$cohab[cov\$marstat==6]<-1
cov\$cohab[cov\$marstat<=5]<-0

cov
#>   marstat nmar mar sep div wid cohab
#> 1       1    1   0   0   0   0     0
#> 2       2    0   1   0   0   0     0
#> 3       3    0   0   1   0   0     0
#> 4       4    0   0   0   1   0     0
#> 5       5    0   0   0   0   1     0
``````

Created on 2019-03-11 by the reprex package (v0.2.1)

dear @andresrcs
thank you, i'll try it

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.