简体   繁体   English

R如何创建动态data.frame?

[英]R how to create dynamic data.frame?

I'm new to R, and I would like to create a dynamic data.frame. 我是R的新手,我想创建一个动态的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. PC变量是城市代码,COUN是县代码。 COMMUN is the concatenation of PC with COUN and is the district ID. COMMUN是PC与COUN的串联,是区ID。 BUILD is the building number, HOUSING indicates homes within a building and PERSON : number of person living in a housing. BUILD是建筑物号,HOUSING表示建筑物内的房屋,PERSON:居住在房屋中的人数。 All variables are character format. 所有变量均为字符格式。

There's more than 200 000 people and 2 000 districts in my table. 我的桌子上有20万人和2 000个地区。

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). 在Server.RI中,有3个输入框来选择我们要显示数据的区域:input $ com,input $ quar(COUN的前两个字符),input $ dis(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. PS:对不起,我的英语不好。

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 : 这是我想获取的data.frame(每个区的居民数),它只是每个区的行数:

 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. 就像Mike在评论中说的那样, dplyr是必经之路。

You receive the dataframe you want by: 您可以通过以下方式收到所需的数据框:

library(dplyr)

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

df is your dataframe example. df是您的数据框示例。 To make your data.frame dynamic related on your input, you should go on reading about reactive elements in shiny. 为了使data.frame与输入动态相关,您应该继续阅读闪亮的反应性元素。 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 ). 返回data将按照您在过滤器小部件中设置的方式进行过滤(在此示例中是filter1filter2 )。 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. 数据帧df可以像普通帧一样使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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