2016-07-21 5 views
0

における(グルーピングと)カテゴリ応答のパーセントを計算します3つの異なる測定値のうち、「形状」は2)で構成されています。行方不明/異なる/ユニーク/同じ:は、私は、次のデータフレームを有するR

各データポイントは4つの可能なカテゴリ値のいずれかを持っています。 「Missing」は、そのデバイスの場合はその測定の値がないことを意味し、他の3つの値はその測定の既存の結果を表します。

質問:私はそれを含まない(すなわちIVの値の合計数のうちの(従って、3つの異なるパーセンテージを生成する)と同じ/異なる/一意の値を有することをデバイスごとに時間のパーセントを計算します「欠落」値がある場合)。

例えば、デバイス2は、以下のパーセンテージを有するであろう:同じ

  • 色 - 67%、0%異なる、独特の33%。
  • 形状-100%同一、0%異なる、0%ユニーク。

ありがとうございました!

答えて

1

これはTIDYソリューションではありませんが、あなたは誰か他の人の記事まで、これを使用することができ、より良い1:

最終的には2つのデータフレームのリストである

# Replace all "Missing" with NAs 
df[df == "Missing"] <- NA 


# Create factor levels 
df[,-1] <- lapply(df[,-1], function(x) { 
     factor(x, levels = c('Same', 'Different', 'Unique')) 
}) 


# Custom function to calculate percent of categorical responses 
custom <- function(x) { 
     y <- length(na.omit(x)) 
     if(y > 0) 
       return(round((table(x)/y)*100)) 
     else 
       return(rep(0, 3)) 
} 


library(purrr) 

# Split the dataframe on IV, remove the IV column and apply the custom function 
Final <- df %>% split(df$IV) %>% 
    map(., function(x) { 
     x <- x[, -1] 
     t(sapply(x, custom)) 
    }) 

出力:

$Color 
     Same Different Unique 
Device1 67  33  0 
Device2 67   0  33 
Device3 0   0  0 

$Shape 
     Same Different Unique 
Device1 50  50  0 
Device2 100   0  0 
Device3 50  50  0 

データ

structure(list(IV = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("Color", 
"Shape"), class = "factor"), Device1 = structure(c(1L, 2L, 1L, 
1L, 2L), .Label = c("Same", "Different", "Unique"), class = "factor"), 
    Device2 = structure(c(1L, 1L, 3L, NA, 1L), .Label = c("Same", 
    "Different", "Unique"), class = "factor"), Device3 = structure(c(NA, 
    NA, NA, 1L, 2L), .Label = c("Same", "Different", "Unique" 
    ), class = "factor")), .Names = c("IV", "Device1", "Device2", 
"Device3"), row.names = c(NA, -5L), class = "data.frame") 
+0

これはあなたに感謝し、完全に働きました。 – Thredolsen

1

クイックや汚れ:まず、「NA」は、お好みの方法(SED、エクセルなど)を使用して、あなたの「行方不明」を置き換え、その後、あなたは要約統計量を得るために、各列の表を使用することができますが:

myStats <- function(x){ 
    table(factor(x, levels = c('Same', 'Different', 'Unique')))/sum(table(x)) 
}  
apply(yourData, 2, myStats) 

これは、必要な要約を返します。

関連する問題