简体   繁体   中英

R how to create dynamic data.frame?

I'm new to R, and I would like to create a dynamic data.frame.

Please let me explain.

I have this table :

 PC;COUN;COMMUN;BUILD;HOUSING;PERSON;SEX
 01;0101;010101;  001;     01;   001;  1
 01;0101;010101;  001;     01;   002;  1
 01;0101;010101;  001;     02;   001;  2
 01;0101;010101;  001;     03;   001;  1
 01;0101;010101;  002;     01;   001;  2
 01;0101;010101;  002;     01;   002;  1
 01;0101;010101;  002;     02;   001;  1
 01;0101;010101;  002;     02;   002;  2
 01;0101;010101;  002;     02;   003;  2
 01;0102;010102;  001;     01;   001;  1
 01;0102;010102;  001;     01;   002;  2
 01;0102;010102;  001;     01;   003;  1
 01;0102;010102;  002;     01;   001;  2
 01;0102;010102;  002;     01;   002;  2
 01;0102;010102;  002;     01;   003;  1
 01;0102;010102;  003;     01;   001;  1
 01;0102;010102;  003;     02;   001;  1
 01;0102;010102;  003;     02;   002;  2
 01;0102;010102;  003;     03;   001;  1
 01;0102;010102;  003;     03;   002;  2

The PC variable is the City code, COUN is the county code. COMMUN is the concatenation of PC with COUN and is the district ID. BUILD is the building number, HOUSING indicates homes within a building and PERSON : number of person living in a housing. All variables are character format.

There's more than 200 000 people and 2 000 districts in my table.

In the Server.RI have 3 input boxes to select the district we want to display datas : input$com, input$quar (2 first characters of COUN), input$dis (2 last characters of COUN).

I would like to create a main data frame including datas of each district.So, I have created a data frame with this command :

 dfDistrict <- data.frame(
        Districts = c(unique(BI14$COMMUN [BI14$PC == input$com &
                      stri_sub(BI14$COUN,1,2) == input$quar]))

Thanks to this command, I get a data frame where each row shows a district. The second variable of the data frame is the number of resident for each district.

But I don't know how to do that. Maybe I have to use "aggregate" ?

Could you please help me ?

Thanks much.

PS : sorry for my bad English.

EDIT :

Here is the data.frame I would like to get (number of residents per district), it's just the number of rows of each district :

 Districts   Residents_Nb
   010101         9
   010102        11

Of course, I will add many others variables after that.

I tried that but it does not work :

 dfDistrict <- data.frame(
    Districts = group_by(COMMUN))

and I tried that too :

 dfDistrict <- data.frame(
    Districts = aggregate(myTable, by=list(myTable$COMMUN), FUN=mean,
    na.rm=TRUE))

As I think "mean" is wrong.

Thank you very much.

Like Mike said in the comments, dplyr is the way to go.

You receive the dataframe you want by:

library(dplyr)

result <- df %>%
  group_by(COMMUN) %>%
  summarize(total=n())

df is your dataframe example. To make your data.frame dynamic related on your input, you should go on reading about reactive elements in shiny. In your case, this could look like:

filteredData <- reactive({
  data <- filter(
    df, df$col1 %in% input$filter1 & df$col2 %in% input$filter2
  )
})

The return data will be filtered the way you set it up in the filter widgets (in this example filter1 and filter2 ). Attention: You need to add brackets to the created dataset when you allocate it to another variable. For example:

df <- filteredData()

The dataframe df can be used like a normal one then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM