I'm struggling with a data transformation in R. The data I receive is of this type:
input <- data.frame(AF = sample(0:1, 100, replace=TRUE),
CAD = sample(0:1, 100, replace=TRUE),
CHF = sample(0:1, 100, replace=TRUE),
DEM = sample(0:1, 100, replace=TRUE),
DIAB = sample(0:1, 100, replace=TRUE))
input$Counts <- rowSums(input)
And the output I'm trying to achieve is:
output <- data.frame(Condition = c('AF', 'CAD', 'CHF', 'DEM', 'DIAB'),
'1' = sample(11:20, 5, replace=TRUE),
'2' = sample(11:20, 5, replace=TRUE),
'3' = sample(11:20, 5, replace=TRUE),
'4' = sample(11:20, 5, replace=TRUE),
'5' = sample(11:20, 5, replace=TRUE))
Where the intersections are the counts of observations matching the condition (which are now in the first column) and the row sums (which are now separate columns).
My solution is below but I'm wondering if there is a more elegant solution?
data.frame(Condition = colnames(input[ ,1:5]),
"One" = c(nrow(input[input$AF==1 & input$Counts==1,]),
nrow(input[input$CAD==1 & input$Counts==1,]),
nrow(input[input$CHF==1 & input$Counts==1,]),
nrow(input[input$DEM==1 & input$Counts==1,]),
nrow(input[input$DIAB==1 & input$Counts==1,])),
"Two" = c(nrow(input[input$AF==1 & input$Counts==2,]),
nrow(input[input$CAD==1 & input$Counts==2,]),
nrow(input[input$CHF==1 & input$Counts==2,]),
nrow(input[input$DEM==1 & input$Counts==2,]),
nrow(input[input$DIAB==1 & input$Counts==2,])),
"Three" = c(nrow(input[input$AF==1 & input$Counts==3,]),
nrow(input[input$CAD==1 & input$Counts==3,]),
nrow(input[input$CHF==1 & input$Counts==3,]),
nrow(input[input$DEM==1 & input$Counts==3,]),
nrow(input[input$DIAB==1 & input$Counts==3,])),
"Four" = c(nrow(input[input$AF==1 & input$Counts==4,]),
nrow(input[input$CAD==1 & input$Counts==4,]),
nrow(input[input$CHF==1 & input$Counts==4,]),
nrow(input[input$DEM==1 & input$Counts==4,]),
nrow(input[input$DIAB==1 & input$Counts==4,])),
"Five" = c(nrow(input[input$AF==1 & input$Counts==5,]),
nrow(input[input$CAD==1 & input$Counts==5,]),
nrow(input[input$CHF==1 & input$Counts==5,]),
nrow(input[input$DEM==1 & input$Counts==5,]),
nrow(input[input$DIAB==1 & input$Counts==5,])),
"Six" = c(nrow(input[input$AF==1 & input$Counts==6,]),
nrow(input[input$CAD==1 & input$Counts==6,]),
nrow(input[input$CHF==1 & input$Counts==6,]),
nrow(input[input$DEM==1 & input$Counts==6,]),
nrow(input[input$DIAB==1 & input$Counts==6,]))
)