2016-07-25 7 views
1

dplyrを使用してカウントの列を作成すると、カウント列を単独で使用するまで正しく塗りつぶされているように見えます。 例: 私はこのデータフレームを作成:私は数の列を作成するためにdplyrを使用し、その後個々のフィールドまたは列として印刷された列は、データフレーム全体が印刷されたときに塗りつぶされます。

V1 <- c("TEST", "test", "tEsT", "tesT", "TesTing", "testing","ME-TESTED", "re tested", "RE testing") 
V2 <- c("othertest", "anothertest", "testing", "123", "random stuff", "irrelevant", "tested", "re-test", "tests") 
V3 <- c("type1", "type2", "type1", "type2", "type3", "type2", "type2", "type2", "type1") 
df <- data.frame(V1, V2, V3) 

を:

df$counts <- df %>% group_by(V3) %>% mutate(count = n()) 

これは期待された結果得られます。

> df 
     V1   V2 V3 counts.V1 counts.V2 counts.V3 counts.count 
1  TEST othertest type1  TEST othertest  type1   3 
2  test anothertest type2  test anothertest  type2   5 
3  tEsT  testing type1  tEsT  testing  type1   3 
4  tesT   123 type2  tesT   123  type2   5 
5 TesTing random stuff type3 TesTing random stuff  type3   1 
6 testing irrelevant type2 testing irrelevant  type2   5 
7 ME-TESTED  tested type2 ME-TESTED  tested  type2   5 
8 re tested  re-test type2 re tested  re-test  type2   5 
9 RE testing  tests type1 RE testing  tests  type1   3 

しかし、を何とかcounts.countカラムを使用しようとすると、結果はnullになります。

> df$counts.count 
NULL 

dplyrによって作成された他の列と同じ結果です。 しかし、データフレームの残りの部分は通常のようだ:

> df$V1 
[1] TEST  test  tEsT  tesT  TesTing testing ME-TESTED re tested RE testing 
Levels: ME-TESTED re tested RE testing test tesT tEsT TEST testing TesTing 

私は、全体のDFを印刷することは私に興味のちょうど列を印刷するよりも異なる出力を与える理由について完全に混乱しています。私はここで何が欠けていますか?

+2

'df $は<-'をカウントし、' df < - 'はカウントしないのはなぜですか?そのような方法で列内に 'data.frame'を作成しています。その列を選択したい場合は、 'df $ count $ count'を実行すると、' data.frame'を2行にサブセット化する必要があります。 –

+0

構文が間違っていて、新しい列を作成する必要があると思いました。データフレーム内にデータフレームを作成した場合、それは説明されますが、dfを出力するときには通常の列のように見えますが、df $ counts.countを出力するときにはNULLになります。 – Thoughtcraft

+1

これは、Rがその中にdata.frameを含む列をどのように表示するかです –

答えて

1

あなたは巻き戻し、データフレームを再作成して、割り当てを行いませんが、ちょうどあなたがこれを見る画面に結果を印刷する場合:あなたは今assgnmentを行うと構造がかなり混乱している

df %>% group_by(V3) %>% mutate(count = n()) 

Source: local data frame [9 x 4] 
Groups: V3 [3] 

      V1   V2  V3 count 
     <fctr>  <fctr> <fctr> <int> 
1  TEST othertest type1  3 
2  test anothertest type2  5 
3  tEsT  testing type1  3 
4  tesT   123 type2  5 
5 TesTing random stuff type3  1 
6 testing irrelevant type2  5 
7 ME-TESTED  tested type2  5 
8 re tested  re-test type2  5 
9 RE testing  tests type1  3 

df$counts <- df %>% group_by(V3) %>% mutate(count = n()) 
# snipped what you already showed 
str(df) 
#----- 
'data.frame': 9 obs. of 4 variables: 
$ V1 : Factor w/ 9 levels "ME-TESTED","re tested",..: 7 4 6 5 9 8 1 2 3 
$ V2 : Factor w/ 9 levels "123","anothertest",..: 4 2 8 1 5 3 7 6 9 
$ V3 : Factor w/ 3 levels "type1","type2",..: 1 2 1 2 3 2 2 2 1 
$ counts:Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 9 obs. of 4 variables: 
    ..$ V1 : Factor w/ 9 levels "ME-TESTED","re tested",..: 7 4 6 5 9 8 1 2 3 
    ..$ V2 : Factor w/ 9 levels "123","anothertest",..: 4 2 8 1 5 3 7 6 9 
    ..$ V3 : Factor w/ 3 levels "type1","type2",..: 1 2 1 2 3 2 2 2 1 
    ..$ count: int 3 5 3 5 1 5 5 5 3 
    ..- attr(*, "vars")=List of 1 
    .. ..$ : symbol V3 
    ..- attr(*, "labels")='data.frame': 3 obs. of 1 variable: 
    .. ..$ V3: Factor w/ 3 levels "type1","type2",..: 1 2 3 
    .. ..- attr(*, "vars")=List of 1 
    .. .. ..$ : symbol V3 
    .. ..- attr(*, "drop")= logi TRUE 
    ..- attr(*, "indices")=List of 3 
    .. ..$ : int 0 2 8 
    .. ..$ : int 1 3 5 6 7 
    .. ..$ : int 4 
    ..- attr(*, "drop")= logi TRUE 
    ..- attr(*, "group_sizes")= int 3 5 1 
    ..- attr(*, "biggest_group_size")= int 5 

あなたが見ているフォーマットは、Rは、データフレームに埋め込まれているマトリックスを表示する方法です:私は、V1またはV2の少ない一意の値があった場合は、より多くの有益なエラーを得ているかもしれないと思います。 table(おそらくtbl?)のオブジェクトは、matrixクラスから継承します。

関連する問題