楼主: daishen
1128 2

[R] 〖R语言〗参议员协作网络部分的代码精简 [推广有奖]

  • 0关注
  • 10粉丝

已卖:127份资源

博士生

6%

还不是VIP/贵宾

-

威望
0
论坛币
3155 个
通用积分
51.3742
学术水平
67 点
热心指数
68 点
信用等级
42 点
经验
3733 点
帖子
111
精华
0
在线时间
324 小时
注册时间
2014-9-14
最后登录
2016-3-10

楼主
daishen 发表于 2015-2-12 14:46:25 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
      

书籍来源+源代码+简化代码部分





      Automated Data Collection with R 第一二章的代码烦的一笔,花了一天的时间对代码进行了优化。


书籍来源
[书籍介绍] Automated Data Collection with R
本文来自: 人大经济论坛 R语言论坛 版,详细出处参考:
https://bbs.pinggu.org/forum.php?mod=viewthread&tid=3318359&page=1&from^^uid=3495972


——素质版主  补充

1、源代码



  1. library(rvest)
  2. library(RCurl)

  3. #Generate empty folder
  4. dir.create("Bills_111/")

  5. # Iterate over all 4059 pieces of legislation
  6. for(i in 1:4059){
  7.     # Generate the unique URL for each piece of legislation
  8.     url <- str_c(
  9.         "http://thomas.loc.gov/cgi-bin/bdquery/D?d111:",
  10.         i,
  11.         ":./list/bss/d111SN.lst:@@@P"
  12.     )
  13.     # Download the page
  14.     bill.result <- getURL(url)
  15.     # Write the page to local hard drive
  16.     write(
  17.         bill.result, str_c(
  18.             "Bills_111/Bill_111_S",
  19.             i,
  20.             ".html"
  21.         )
  22.     )
  23.     # Print progress of download
  24.     cat(i, "\n")
  25. }
复制代码

2、简化部分






  1. myfun = function(i) {
  2.              url = str_c("Bills_111/Bill_111_S", i, ".html")
  3.              sponsors = url %>% html() %>% html_nodes("div a") %>% html_text() %>% .[[15]] %>% gsub("Sen","",.)
  4.              cosponsors = url %>% html() %>% html_nodes("p a") %>% html_text() %>% .[-1] %>% gsub("Sen","",.)
  5.              list(sponsors,cosponsors)
  6.             }

  7. sponsor.list = lapply(1:4059,myfun)

  8. sponsor.list[[1]][1] = " Reid, Harry"

  9. all.senators = sponsor.list %>% unlist %>% unique %>% sort %>% .[-length(.)] #109个参议员

  10. # Create a matrix of sponsors

  11. sponsor.matrix = matrix(NA, nrow = 4059, ncol = length(all.senators))
  12. colnames(sponsor.matrix) = all.senators
  13. rownames(sponsor.matrix) = paste("S.", seq(1, 4059), sep ="")

  14. for(i in 1:length(sponsor.list)){
  15.     sponsor.matrix[i, which(all.senators == sponsor.list[][1])] = "Sponsor"
  16.     sponsor.matrix[i, which(all.senators %in% unlist(sponsor.list[][2]))] = "Cosponsor"
  17.     }

  18. > sponsor.matrix[30:35,31:34]
  19.       Cornyn, John  Crapo, Mike  DeMint, Jim  Dodd, Christopher J.
  20. S.30 NA            NA           NA           NA                  
  21. S.31 NA            NA           NA           NA                  
  22. S.32 NA            NA           NA           NA                  
  23. S.33 NA            NA           NA           NA                  
  24. S.34 "Cosponsor"   "Cosponsor"  "Sponsor"    NA                  
  25. S.35 "Cosponsor"   NA           NA           NA  
复制代码




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:R语言 协作网 参议员 Legislation christopher 参议员 网络 参议员 网络

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
niuniuyiwan + 60 + 36 + 2 + 2 + 2 精彩帖子
我的素质低 + 10 + 2 + 2 精彩帖子

总评分: 经验 + 60  论坛币 + 46  学术水平 + 4  热心指数 + 4  信用等级 + 2   查看全部评分

本帖被以下文库推荐

沙发
niuniuyiwan 在职认证  发表于 2015-8-12 19:03:12
好帖,感谢分享。

藤椅
Alfred_G 学生认证  发表于 2015-9-1 19:08:58
great~

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 00:29