2016-11-16 6 views
-1

"A"、 "B"、 "C"などの結果を持つ100列(名前はCol_1、Col_2 ... Col_100)のデータセットがあります。私は、多くの異なる文字がすべてのデータセットにあることを知らない。私はこれをしようとしているPolynominalをBinominalに変換する - 何千もの列

A B C D 
0 1 0 1 
1 1 0 1 

library(reshape2) 
train <- read.csv("train.csv",head=TRUE,sep=",") 
train 

recast(train, id ~ value, id.var = 1, fun.aggregate = function(x) (length(x) > 0) + 0L) 

しかし、私は次のエラーが取得しています:

を私のような行列を持つように列にそれぞれの値を変換しようとしています
Error in eval(substitute(expr), envir, enclos) : 
    n must be a positive integer 
In addition: Warning messages: 
1: attributes are not identical across measure variables; they will be dropped 
2: In split_indices(.group, .n) : 
    NAs introduced by coercion to integer range 

私が望むテーブルを返すために何ができるのですか?

答えて

0

これはおそらくあなたが探しているものです。最初のステップは、可能な値を収集します。 2番目のステップでは、各変数に潜在的な値を認識させます。これにより、rbindが適切な出力を構成するように、特定の値が欠けているときにtableが0カウントを生成することができます。ご回答に感謝@Imo

# collect all possible values 
allLevels <- levels(unlist(sapply(df, unique))) 
# provide all levels to each variable in the data.frame 
dfNew <- data.frame(lapply(df, function(i) factor(i, levels=allLevels))) 

# produce the count for each variable 
do.call(rbind, lapply(dfNew, table)) 
    a b c d e g i j 
x 3 2 8 2 0 0 0 0 
y 0 0 2 4 4 1 3 1 

データ

set.seed(1234) 
df <- data.frame(x=sample(letters[1:4], 15, replace=TRUE), 
       y=sample(letters[3:10], 15, replace=TRUE)) 
+0

。 N/Aとしてすべての値を取得していますが、正常ですか? –

+0

私が提供した例、または元のデータセットを使用していますか?元のデータセットであれば、最低でも 'str(df)'の最初の10行を指定します。ここで、dfはdata.frameの名前です。 – lmo

+0

どうすればいいですか? R –

関連する問題