2017-09-21 9 views
0

I次のデータセットがあります。dplyr ::数()複数の列

dat = structure(list(C86_1981 = c("Outer London", "Buckinghamshire", 
NA, "Ross and Cromarty", "Cornwall and Isles of Scilly", NA, 
"Kirkcaldy", "Devon", "Kent", "Renfrew"), C96_1981 = c("Outer London", 
"Buckinghamshire", NA, "Ross and Cromarty", "Not known/missing", 
NA, "Kirkcaldy", NA, NA, NA), C00_1981 = c("Outer London", "Inner London", 
"Lancashire", "Ross and Cromarty", NA, "Humberside", "Kirkcaldy", 
NA, NA, NA), C04_1981 = c("Kent", NA, NA, "Ross and Cromarty", 
NA, "Humberside", "Not known/missing", NA, NA, "Renfrew"), C08_1981 = c("Kent", 
"Oxfordshire", NA, "Ross and Cromarty", "Cornwall and Isles of Scilly", 
"Humberside", "Dunfermline", NA, NA, "Renfrew"), C12_1981 = c("Kent", 
NA, NA, "Ross and Cromarty", "Cornwall and Isles of Scilly", 
"Humberside", "Dunfermline", NA, NA, "Renfrew")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("C86_1981", 
"C96_1981", "C00_1981", "C04_1981", "C08_1981", "C12_1981")) 

私はdplyr::count()各列にしたいの。予想される出力:

library("tidyverse") 

dat86_n = dat %>% 
    count(C86_1981) %>% 
    rename(dat86_n = n) 
dat96_n = dat %>% 
    count(C96_1981) %>% 
    rename(dat96_n = n) 
# ... 

dat_counts = dat86_n %>% 
    full_join(dat96_n, by = c("C86_1981" = "C96_1981")) 
    # ... 

動作しますが、私のデータのいずれかが、後で変更した場合、正確に堅牢ではありません。

# A tibble: 10 x 3 
         C86_1981 dat86_n dat96_n ... 
          <chr> <int> <int> 
1    Buckinghamshire  1  1 
2 Cornwall and Isles of Scilly  1  NA 
3      Devon  1  NA 
4       Kent  1  NA 
5     Kirkcaldy  1  1 
6     Outer London  1  1 
7      Renfrew  1  NA 
8   Ross and Cromarty  1  1 
9       <NA>  2  5 
10   Not known/missing  NA  1 

現在、私は結果をINGの手動dplyr::full_join()これをやっています。私はプログラムでこれをやりたいと思っていました。

私はループを試してみた:

lapply(dat, count) 
# Error in UseMethod("groups") : 
# no applicable method for 'groups' applied to an object of class "character" 

purrr::map()が同じエラーになります)。私はcount()tblと別々の引数として変数を期待しているため、このエラーがあると思うので、私はそれも試してみました:

lapply(dat, function(x) { 
    count(dat, x) 
}) 
# Error in grouped_df_impl(data, unname(vars), drop) : 
# Column `x` is unknown 

ここでも、purrr::map()は同じエラーを与えます。私はまた、summarise_all()のバリエーションを試してみた:

dat %>% 
    summarise_all(count) 
    # Error in summarise_impl(.data, dots) : 
    # Evaluation error: no applicable method for 'groups' applied to an object of class "character". 

私は何かを明らかに不足していることだし、解決策は簡単でなければなりませんように私は感じます。 dplyrこれは私が最も利用する傾向があるため、特に歓迎します。またtidyrパッケージを使用して

答えて

2

、次のコードは、トリックを行います:

dat %>% tidyr::gather(name, city) %>% dplyr::group_by(name, city) %>% dplyr::count() %>% dplyr::ungroup %>% tidyr::spread(name, n) 

結果:-leee @

# A tibble: 15 x 7 
          city C00_1981 C04_1981 C08_1981 C12_1981 C86_1981 C96_1981 
*      <chr> <int> <int> <int> <int> <int> <int> 
1    Buckinghamshire  NA  NA  NA  NA  1  1 
2 Cornwall and Isles of Scilly  NA  NA  1  1  1  NA 
3      Devon  NA  NA  NA  NA  1  NA 
4     Dunfermline  NA  NA  1  1  NA  NA 
5     Humberside  1  1  1  1  NA  NA 
6     Inner London  1  NA  NA  NA  NA  NA 
7       Kent  NA  1  1  1  1  NA 
8     Kirkcaldy  1  NA  NA  NA  1  1 
9     Lancashire  1  NA  NA  NA  NA  NA 
10   Not known/missing  NA  1  NA  NA  NA  1 
11     Outer London  1  NA  NA  NA  1  1 
12     Oxfordshire  NA  NA  1  NA  NA  NA 
13      Renfrew  NA  1  1  1  1  NA 
14   Ross and Cromarty  1  1  1  1  1  1 
15       <NA>  4  5  3  4  2  5 
+0

はい!ありがとうございました!一つの微調整: 'ungroup()'がなくても 'count()'があなたのために行うようになりますが、そうでなければ完璧です。 – Phil

2

ちょうどそれを私にビート;)を使用して

tidyverse;

library(tidyverse) 

df <- 
    dat %>% 
    gather (year, county) %>% 
    group_by(year, county) %>% 
    summarise(no = n()) %>% 
    spread (year, no) 

# A tibble: 15 x 7 
         county C00_1981 C04_1981 C08_1981 C12_1981 C86_1981 C96_1981 
*      <chr> <int> <int> <int> <int> <int> <int> 
1    Buckinghamshire  NA  NA  NA  NA  1  1 
2 Cornwall and Isles of Scilly  NA  NA  1  1  1  NA 
3      Devon  NA  NA  NA  NA  1  NA 
4     Dunfermline  NA  NA  1  1  NA  NA 
5     Humberside  1  1  1  1  NA  NA 
6     Inner London  1  NA  NA  NA  NA  NA 
7       Kent  NA  1  1  1  1  NA 
8     Kirkcaldy  1  NA  NA  NA  1  1 
9     Lancashire  1  NA  NA  NA  NA  NA 
10   Not known/missing  NA  1  NA  NA  NA  1 
11     Outer London  1  NA  NA  NA  1  1 
12     Oxfordshire  NA  NA  1  NA  NA  NA 
13      Renfrew  NA  1  1  1  1  NA 
14   Ross and Cromarty  1  1  1  1  1  1 
15       <NA>  4  5  3  4  2  5 
+1

まだupvoteの価値があります。ありがとう:) – Phil

+0

ありがとう@Phil、常にポイントを評判を向上させる必要があります! ;) – sorearm

関連する問題