2016-09-17 23 views
0

我已經posted my problem因爲幾個月,認爲一切都好,但似乎沒有。我很抱歉多個帖子。加權數據 - 不一致的結果再次

DF=structure(list(Age = c(16L, 29L, 22L, 64L, 42L, 46L, 30L, 37L, 31L, 52L, 44L, 54L, 23L, 22L, 42L, 39L, 39L, 51L, 25L, 64L, 55L, 56L, 27L, 31L, 39L, 22L, 54L, 33L, 34L, 18L, 39L, 41L, 52L, 41L, 27L, 36L, 64L, 42L, 21L, 44L, 50L, 35L, 22L, 65L, 53L, 18L, 25L, 59L, 56L, 52L, 39L, 40L, 25L, 63L, 43L, 23L, 52L, 48L, 24L, 45L, 27L, 42L, 56L, 43L, 28L, 51L, 54L, 16L, 65L, 56L, 47L, 45L, 29L, 41L, 52L, 50L, 26L, 44L, 35L, 55L, 57L, 43L, 52L, 28L, 33L, 20L, 39L, 15L, 55L, 20L, 30L, 10L, 54L, 51L, 47L, 36L, 42L, 33L, 26L, 29L, 19L, 22L, 22L, 22L, 40L, 33L, 20L, 43L, 53L, 25L, 25L, 49L, 25L, 31L, 45L, 51L, 60L, 54L, 20L, 25L, 60L, 48L, 35L, 42L, 14L, 28L, 55L, 20L, 35L, 17L, 46L, 20L, 45L, 37L, 33L, 36L, 60L, 47L, 27L, 25L, 51L, 32L, 19L, 25L, 19L, 60L, 18L, 17L, 33L, 26L, 33L, 32L, 33L, 22L, 17L, 24L, 43L, 38L, 27L, 40L, 42L, 41L, 31L, 43L, 34L, 33L, 42L, 37L, 24L, 50L, 53L, 35L, 50L, 37L, 46L, 39L, 33L, 56L, 58L, 23L, 31L, 52L, 50L, 33L, 56L, 55L, 20L, 22L, 44L, 50L, 30L, 58L, 59L, 16L, 33L, 53L, 50L, 20L, 31L, 22L, 38L, 59L, 38L, 62L, 52L, 30L, 18L, 53L, 38L, 41L, 44L, 53L, 19L, 53L, 57L), 
    Sous_Categorie = c("7", "7", "7", "7", "7", "7", "7", "7", 
    "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", 
    "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", 
    "7", "7", "7", "7,9", "8", "8", "8", "8", "8", "9", "9", 
    "11", "10,7", "10,8,9", "7", "7", "7", "7", "7", "7,8", "8", 
    "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "8", 
    "11", "7", "12", "12", "12", "12", "12", "12", "12", "12", 
    "12", "12", "12", "12", "12", "12", "12", "12", "13", "13", 
    "13", "13", "13", "14", "14", "14", "14", "14", "14", "14", 
    "14", "14", "14", "14", "14", "14", "14", "14", "14", "14", 
    "14", "14", "14", "15", "15", "15", "15", "15", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "17", "17", "17", "17", "17", 
    "17", "17", "17", "17", "17", "18", "18", "18", "7,12", "7,12", 
    "7,12", "7,12", "7,13,17", "7,16", "7,17", "7,17", "7,17", 
    "7,17", "7,17", "8,17", "8,17", "11,17", "7,17", "7,17", 
    "8,17", "7,17", "7,17", "12,14", "12,15", "17,18")), .Names = c("Age", "Sous_Categorie"), row.names = c(NA, -215L), class = "data.frame") 

而這裏提出和接受的解決方案:

cat_perc <- function(vec) { 
    # percentages 
    nums <- table(as.numeric(unlist(strsplit(vec, ',')))) 
    perc <- nums/sum(nums) 
    final <- perc * length(vec) 
    return(final) 
} 

    cat_perc(DF$Sous_Categorie) 

我剛剛檢查結果,我使用這個腳本,因爲許多星期,但我注意到的是,權重似乎是假的:如果沒有組合,則目標是歸因於1,如果組合使用逗號分隔:1,3,則每個組合爲0.5和0.5,對於1,2,3組合,它將是1/3 ,1/3,1/3。我手動檢查7,這個腳本表明我61.9341564,而手動我有:52 * 1 + 0.5 * 17 +(1/3 * 1)= 60.83333,問題在哪裏:(?謝謝

+0

你爲什麼不問問誰回答了這個問題的人? –

+1

@alistaire請檢查出這個問題 –

+0

因爲這個腳本是通過兩個職位建立..是否有任何其他最簡單的解決方案?我很抱歉.. – ranell

回答

0

最後,感謝的計算器我有下面的代碼很多帖子:

library(dplyr) 
library(reshape2) 
datas=DF 

cat_perc <- function(vec) { 
# split strings 
L <- lapply(vec,function(v) strsplit(as.character(v),',')) 
# generate all combinations of products 
d <- mapply(expand.grid,L,SIMPLIFY = F,stringsAsFactors=F) 
proportion <- melt(d,id.vars=c('Var1')) %>% # convert to long format 
    group_by(L1) %>% 
    mutate(weight=1/n()) %>%    # calculate weights 
    group_by(Var1) %>%    # 
    summarize(sm=sum(weight))  
proportion=t(proportion) 
colnames(proportion)=proportion[1,] 
proportion=proportion[-1,] 
return(proportion) 
} 

cat_perc(DF$Sous_Categorie) 

的重量似乎是確定問題仍然存在:。重塑的最後三個行也不行,我不要不知道如何獲得相同的格式比initial_function

這是輸出:

output= structure(c(" 0.8333333", " 2.5000000", "19.0000000", " 5.3333333", 
"20.5000000", " 5.5000000", " 0.5000000", "84.3333333", " 3.5000000", 
"60.8333333", " 9.3333333", " 2.8333333"), .Names = c("10", "11", 
"12", "13", "14", "15", "16", "17", "18", "7", "8", "9")) 

我需要的格式/類output作爲initial_function輸出:

initial_function=structure(c(61.9341563786008, 10.6172839506173, 3.53909465020576, 
1.76954732510288, 2.65432098765432, 19.4650205761317, 5.30864197530864, 
18.5802469135802, 5.30864197530864, 0.88477366255144, 81.3991769547325, 
3.53909465020576), class = "table", .Dim = 12L, .Dimnames = structure(list(
    c("7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
    "17", "18")), .Names = "")) 

感謝