2017-04-25 61 views
1

這是發病率數據。按農場和年份合併的死亡和風險時間合併 我試圖使用方法= 「cohort.time」(epiR軟件包)將數據轉換爲epi.2by2所需的格式。R數據框到3維的表格

示例數據:

test <- rbind(c(12, 2,0), c(29,16,26), c(6941, 6083, 5051), c(4555, 5148, 3608)) 
colnames(test) <- c(3, 3.5, 4) 
rownames(test) <- c("deaths-unexposed", "deaths-exposed", "timeatrisk-unexposed", "timeatrisk-exposed") 

farm-year    3 3.5 4 
deaths-unexposed  12 2 0 
deaths-exposed   29 16 26 
timeatrisk-unexposed 6941 6083 5051 
timeatrisk-exposed 4555 5148 3608 

需要輸出:

, , farm_year = 3 

      deaths timeatrisk 
exposed  
     1  29  6941 
     0  12  4555 

    , , farm_year = 3.5 

      deaths timeatrisk 
exposed  
     1  16  6083 
     0  2  6148 

我試着用表()在幾個方面,但它給了我很多的0的延長表每個farm_year階層。我相信這是一個簡單的答案(重塑?xtabs?)我找不到它!

回答

1

這有點改寫(munging)的:

library(tidyverse) 

test_tab <- test %>% 
    t() %>% # transpose matrix 
    as.data.frame() %>% 
    rownames_to_column('farm_year') %>% 
    gather(var, val, -farm_year) %>% # reshape to long form 
    separate(var, c('variable', 'exposed')) %>% # separate variables 
    xtabs(val ~ exposed + variable + farm_year, .) 

test_tab 
#> , , farm_year = 3 
#> 
#>   variable 
#> exposed  deaths timeatrisk 
#> exposed  29  4555 
#> unexposed  12  6941 
#> 
#> , , farm_year = 3.5 
#> 
#>   variable 
#> exposed  deaths timeatrisk 
#> exposed  16  5148 
#> unexposed  2  6083 
#> 
#> , , farm_year = 4 
#> 
#>   variable 
#> exposed  deaths timeatrisk 
#> exposed  26  3608 
#> unexposed  0  5051 
+0

這確實是 - 我會tidyverse添加到我的包劇目! – CaitlinP

1

使用data.table

test <- as.data.table(t(rbind(c(12, 2,0), c(29,16,26), c(6941, 6083, 5051), c(4555, 5148, 3608)))) 

colnames(test) <- c("deaths-unexposed", "deaths-exposed", "timeatrisk-unexposed", "timeatrisk-exposed") 
test[, farm_year := c(3, 3.5, 4)] 

result <- melt(test, measure = patterns("^deaths", "timeatrisk"), value.name = c("deaths", "timeatrisk"), variable.name = "Exposed", variable.factor=FALSE) 
result[, Exposed := as.numeric(Exposed) - 1] 

result[farm_year==3] 
    farm_year Exposed deaths timeatrisk 
1:   3  0  12  6941 
2:   3  1  29  4555 

result[farm_year==3.5] 
    farm_year Exposed deaths timeatrisk 
1:  3.5  0  2  6083 
2:  3.5  1  16  5148 
+0

這很好,但我需要它作爲一個表,而不僅僅是子集表來查看。 – CaitlinP