计算观察的百分比分数

时间:2015-11-16 13:21:01

标签: r dataframe

我有一个数据集,其中一定数量的变量构成观察的100%,例如,

        date  x y  z something
1 1998-01-01 12 2  1         a
2 1998-02-01 23 4 90         b
3 1998-03-01 55 6 23         c

合并xyz变量是每次观察的100%(即obs1 x+y+z = 15 = 100%; obs2 x+y+z = 117 = 100%;以及obs3 x+y+z = 84 = 100%

我想计算每个观察值的百分比值,以便数据集看起来像这样:

        date     x     y     z something
1 1998-01-01 80.00  13.3  6.67         a
2 1998-02-01 19.66  3.42 76.92         b
3 1998-03-01 65.47  7.14 27.38         c

其中xyz变量代表比例。 我很难弄清楚如何做到这一点。感谢。

Dput:

structure(list(date = structure(c(10227, 10258, 10286), class = "Date"), 
x = c(12, 23, 55), y = c(2, 4, 6), z = c(1, 90, 23), something = c("a", 
"b", "c")), .Names = c("date", "x", "y", "z", "something"
), row.names = c(NA, 3L), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

或者只是一个简单而有效的基础R解决方案

library(shiny)

ui <- 
pageWithSidebar(

headerPanel("Dynamic table for Input of different types"),

sidebarPanel(
  helpText("number of rows"),
  numericInput("nres","",3,min=0),
  actionButton('create_res',"create",icon=icon("plus"),width='100%'),
  br(),
  br(),
  br(),
  helpText("read input you entered"),
  actionButton('finish_res',"finish",icon=icon("check"),width='100%'),
  width=2
),

mainPanel(  
  h3("input table"),
  uiOutput('matrix_res'),
  h3("output table"),
  tableOutput('check_table'),
  br()

)
)

server <- 
function(input,output){


# input table
# table with different types of input
# row number can be changed with number input
# changes are applied after pressing create button
output$matrix_res <- renderTable({

  input$create_res # dependency


  Col_entries <- c("text input","number input","selection")

  matrix <- data.frame()

  for (i in 1:isolate(input$nres)) {
    matrix[i,1] <- paste0("<input id='element", i, "_", 1, "' class='shiny-bound-input span6' type='text' value='",input[[paste0("element", i, "_", 1)]],"'>") 
    matrix[i,2] <- paste0("<input id='element", i, "_", 2, "' class='shiny-bound-input span6' type='number' value='",input[[paste0("element", i, "_", 2)]],"'>")
    matrix[i,3] <- paste0("<div class='form-group shiny-input-container'><div>
                          <select id='element", i, "_", 3, "' class='form-control'>
                          <option value='a'>a</option>
                          <option value='b'>b</option>
                          </div></div>") 
  }

  colnames(matrix) <- Col_entries

  matrix


  },sanitize.text.function = identity)




# change row number for output table
# only if new input table is created with create button
row_number<-reactive({
  input$create_res # dependency
  isolate(input$nres) 
})




# output table
# object created when clicking on finish button
# only dependent on finish button
output_table<-reactive({

  input$finish_res # dependency

  Col_entries <- c("text input","number input","selection")

  matrix <- data.frame()

  isolate(
    for (i in 1:isolate(row_number())) {
      matrix[i,1] <- input[[paste0('element',i,'_',1)]]
      matrix[i,2] <- input[[paste0('element',i,'_',2)]]
      matrix[i,3] <- input[[paste0('element',i,'_',3)]]
    }
  )
  colnames(matrix) <- Col_entries

  matrix

})


# show output table
output$check_table<-renderTable({
  if(input$finish_res == 0) return() #hide it on start
  output_table()
})

}


runApp(list(ui = ui, server = server))

答案 1 :(得分:1)

library(dplyr)
df <- mutate(df, total = x+y+z, p.x = 100*x/total, p.y = 100*y/total, p.z = 100*z/total)

        date  x y  z something total      p.x       p.y       p.z
1 1998-01-01 12 2  1         a    15 80.00000 13.333333  6.666667
2 1998-02-01 23 4 90         b   117 19.65812  3.418803 76.923077
3 1998-03-01 55 6 23         c    84 65.47619  7.142857 27.380952