简体   繁体   English

如何使用 rbind 向 dataframe R 添加行

[英]How to add rows to dataframe R with rbind

I know this is a classic question and there are also similar ones in the archive, but I feel like the answers did not really apply to this case.我知道这是一个经典问题,档案中也有类似的问题,但我觉得答案并不真正适用于这个案例。 Basically I want to take one dataframe (covid cases in Berlin per district), calculate the sum of the columns and create a new dataframe with a column representing the name of the district and another one representing the total number.基本上我想取一个 dataframe(柏林每个地区的 covid 病例),计算列的总和并创建一个新的 dataframe,其中一列代表地区名称,另一列代表总数。 So I wrote所以我写了

covid_bln <- read.csv('https://www.berlin.de/lageso/gesundheit/infektionsepidemiologie-infektionsschutz/corona/tabelle-bezirke-gesamtuebersicht/index.php/index/all.csv?q=', sep=';')
c_tot<-data.frame('district'=c(), 'number'=c())
for (n in colnames(covid_bln[3:14])){
    x<-data.frame('district'=c(n), 'number'=c(sum(covid_bln$n)))
    c_tot<-rbind(c_tot, x)
    next
}
print(c_tot)

Which works properly with the names but returns only the number of cases for the 8th district, but for all the districts.哪个名称可以正常工作,但仅返回第 8 区的病例数,但返回所有区。 If you have any suggestion, even involving the use of other functions, it would be great.如果您有任何建议,甚至涉及到其他功能的使用,那就太好了。 Thank you谢谢

Here's a base R solution:这是一个base R解决方案:

number <- colSums(covid_bln[3:14])
district <- names(covid_bln[3:14])
c_tot <- cbind.data.frame(district, number)
rownames(c_tot) <- NULL

# If you don't want rownames:
rownames(c_tot) <- NULL

This gives us:这给了我们:

                     district number
1                       mitte  16030
2    friedrichshain_kreuzberg  10679
3                      pankow  10849
4  charlottenburg_wilmersdorf  10664
5                     spandau   9450
6         steglitz_zehlendorf   9218
7       tempelhof_schoeneberg  12624
8                   neukoelln  14922
9           treptow_koepenick   6760
10        marzahn_hellersdorf   6960
11                lichtenberg   7601
12              reinickendorf   9752

I want to provide a solution using tidyverse .我想使用tidyverse提供解决方案。 The final result is ordered alphabetically by districts最终结果按地区字母顺序排列

  c_tot <- covid_bln %>%
    select( mitte:reinickendorf) %>% 
    gather(district, number, mitte:reinickendorf) %>% 
    group_by(district) %>% 
    summarise(number = sum(number))

The rusult is结果是

# A tibble: 12 x 2
   district                   number
 * <chr>                       <int>
 1 charlottenburg_wilmersdorf  10736
 2 friedrichshain_kreuzberg    10698
 3 lichtenberg                  7644
 4 marzahn_hellersdorf          7000
 5 mitte                       16064
 6 neukoelln                   14982
 7 pankow                      10885
 8 reinickendorf                9784
 9 spandau                      9486
10 steglitz_zehlendorf          9236
11 tempelhof_schoeneberg       12656
12 treptow_koepenick            6788

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

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