web-dev-qa-db-ja.com

それらを整数レベルに変換せずに要素を連結する方法は?

Rがベクトルを連結するときに因子を数値に強制変換することを知って驚いた。これは、レベルが同じでも発生します。例えば:

> facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> facs
[1] i       want    to      be      a       factor  not     an      integer
Levels: a an be factor i integer not to want
> c(facs[1 : 3], facs[4 : 5])
[1] 5 9 8 3 1

rでこれを行う慣用的な方法は何ですか(私の場合、これらのベクトルはかなり大きくなる可能性があります)?ありがとうございました。

40
Keith

Rメーリングリスト から:

unlist(list(facs[1 : 3], facs[4 : 5]))

要因を「結合」するには、

data.frame(facs[1 : 3], facs[4 : 5])
36
fgregg

代替の回避策は、因子を文字ベクトルに変換し、連結を完了したときに元に戻すことです。

cfacs <- as.character(facs)
x <- c(cfacs[1:3], cfacs[4:5]) 

# Now choose between
factor(x)
# and
factor(x, levels = levels(facs))
9
Richie Cotton

うわー、私はそれがそれをしたことに気づかなかった。これは回避策です:

x <- c(facs[1 : 3], facs[4 : 5]) 
x <- factor(x, levels=1:nlevels(facs), labels=levels(facs))
x

出力:

[1] i    want to   be   a   
Levels: a an be factor i integer not to want

2つのベクトルのレベルが同じである場合にのみ機能します。

6
Aniko

使用する fct_cforcatsパッケージから(- tidyverse の一部)。

> library(forcats)
> facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> fct_c(facs[1:3], facs[4:5])
[1] i    want to   be   a
Levels: a an be factor i integer not to want

fct_cは、数値コーディングが異なる因子の連結によってだまされません。

> x <- as.factor(c('c', 'z'))
> x
[1] c z
Levels: c z
> y <- as.factor(c('a', 'b', 'z'))
> y
[1] a b z
Levels: a b z
> c(x, y)
[1] 1 2 1 2 3
> fct_c(x, y)
[1] c z a b z
Levels: c z a b
> as.numeric(fct_c(x, y))
[1] 1 2 3 4 2
5
Connor Harris

これは本当に悪いRの落とし穴です。これらの線に沿って、これは私の時間の数時間を飲み込んだばかりのものです。

x <- factor(c("Yes","Yes","No", "No", "Yes", "No"))
y <- c("Yes", x)

> y
[1] "Yes" "2"   "2"   "1"   "1"   "2"   "1"  
> is.factor(y)
[1] FALSE

私には、より良い修正は、キャラクターに強制するRichieの修正であるように見えます。

> y <- c("Yes", as.character(x))
> y
[1] "Yes" "Yes" "Yes" "No"  "No"  "Yes" "No" 
> y <- as.factor(y)
> y
[1] Yes Yes Yes No  No  Yes No 
Levels: No Yes

リッチーが言うように、レベルを適切に設定する限り。

4
Paul Johson

このため、私はdata.frames内の要素を操作することを好みます。

df <- data.frame(facs = as.factor(
      c("i", "want", "to", "be", "a", "factor", "not", "an", "integer") ))

行のインデックスではなく、subset()またはdplyr :: filter()などを使用してサブセット化します。この場合、意味のあるサブセット基準がないため、head()とtail()を使用します。

df1 <- head(df, 4)
df2 <- tail(df, 2)

次に、それらを非常に簡単に操作できます。例:

dfc <- rbind(df1, df2)
dfc$facs
#[1] i       want    to      be      an      integer
#Levels: a an be factor i integer not to want
1
Melkor.cz

文字への変換を使用する他の回答に基づいて、私は次の関数を使用して因子を連結しています:

concat.factor <- function(...){
  as.factor(do.call(c, lapply(list(...), as.character)))
}

この関数は、cと同じように使用できます。

1
snaut

設定が少し異なる場合に因子変数に追加する別の方法を次に示します。

facs <- factor(1:3, levels=1:9,
               labels=c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
facs
# [1] i       want    to      be      a       factor  not     an      integer
# Levels: a an be factor i integer not to want
facs[4:6] <- levels(facs)[4:6]
facs
# [1] i      want   to     be     a      factor
# Levels: i want to be a factor not an integer