0

非常に基本的な質問ですが、検索しても答えが見つかりませんでした。

順序変数の値を新しい値に再コード化しようとしています。

次のように、carパッケージのrecode()関数を使用してみました。

recode(x, "0=1; 1=2; 3=2")

次のエラーメッセージが表示されます。

Error in recode(threecat, "0=1; 1=2; 3=2") : 
  (list) object cannot be coerced to type 'double

'

ご協力いただきありがとうございます。

4

1 に答える 1

3

私にthreecatはリストのように見え、 car::recode はベクトルを期待しています。何が入っていthreecatますか?@mnel の提案に従って、 の結果を含めますdput(head(threecat))

> x<-c(0,1,2,3,4)
> recode(x, "0=1; 1=2; 3=2")
[1] 1 2 2 2 4
> y<-list(x)
> y
[[1]]
[1] 0 1 2 3 4

> recode(y, "0=1; 1=2; 3=2")
Error in recode(y, "0=1; 1=2; 3=2") : 
  (list) object cannot be coerced to type 'double'

threecat にベクトルの要素がある場合、ベクトル要素に対して recode を実行できます。

> recode(y[[1]], "0=1; 1=2; 3=2")
[1] 1 2 2 2 4

threecat が要素のリストである場合は、リストから外す必要があります。

> yy <- list(0,1,2,3,4)
> yy
[[1]]
[1] 0

[[2]]
[1] 1

[[3]]
[1] 2

[[4]]
[1] 3

[[5]]
[1] 4

> recode(unlist(yy), "0=1; 1=2; 3=2")
[1] 1 2 2 2 4

実際に使用している変数を見ずにこれ以上言うのは難しいです。

于 2012-11-28T03:09:06.183 に答える