これは、このデータフレームに、gather
コール結果を説明するために... SO今まで答えが、ここに行く
df1 <- read.table(text = "X|Y| Z
apple|rest|town
town|map|guide
rest|full|down", header = TRUE, sep = "|")
library(reshape2)
library(tidyr)
library(dplyr)
gathered <- gather(df1, value = uniquevalue)
casted <- dcast(gathered, uniquevalue ~ key)
final <- casted %>%
mutate_at(c("X", "Y", "Z"), .funs = function(m) 1 * !is.na(m)) %>%
group_by(uniquevalue) %>%
summarise_all(.funs = sum)
> final
# A tibble: 7 × 4
uniquevalue X Y Z
<chr> <dbl> <dbl> <dbl>
1 apple 1 0 0
2 down 0 0 1
3 full 0 1 0
4 guide 0 0 1
5 map 0 1 0
6 rest 1 1 0
7 town 1 0 1
を醜いのレコードを設定するかもしれません。
> casted
uniquevalue X Y Z
1 apple apple <NA> <NA>
2 down <NA> <NA> down
3 full <NA> full <NA>
4 guide <NA> <NA> guide
5 map <NA> map <NA>
6 rest rest rest <NA>
7 town town <NA> town
最終呼び出しがちょうど1
で0
と、欠損値と非欠損値を置き換えた後、ソートのまとめsquishes /アップ追加:次のような結果に
> gathered
key uniquevalue
1 X apple
2 X town
3 X rest
4 Y rest
5 Y map
6 Y full
7 Z town
8 Z guide
9 Z down
私たちは、その後、dcast
uniquevalue
列。そして、我々が得る:
> final
# A tibble: 7 × 4
uniquevalue X Y Z
<chr> <dbl> <dbl> <dbl>
1 apple 1 0 0
2 down 0 0 1
3 full 0 1 0
4 guide 0 0 1
5 map 0 1 0
6 rest 1 1 0
7 town 1 0 1