2016-04-29 67 views
1

我对金融变量的年度数据,x,像下面这样:添加年率可变数据到现有的面板中的R

x1 <- data.frame(individual = letters, 
        "2001" = rnorm(26, 25, 5), 
        "2002" = rnorm(26, 30, 6), 
        # ... ... 
        "2010" = rnorm(26, 35, 5)) 
head(x1) 
    individual 2001 2002 2010 
1   a 22.88818 31.11008 32.45270 
2   b 29.75727 29.01248 29.43246 
3   c 26.50852 36.94197 38.27126 
4   d 26.70166 20.58665 27.34747 
5   e 29.63059 32.59156 34.56336 
6   f 23.71214 17.40315 34.72396 

转化x s转换长格式reshape2::melt和合并我结束了变量后了一个面板数据,如:

mydata <- data.frame(individual = rep(letters[1:5], each = 5), 
         year = rep(2001:2005, 5), 
         x1 = rnorm(25, 10, 2), 
         x2 = rnorm(25, 30, 5), 
         x3 = rnorm(25, 50, 10)) 
head(mydata) 
    individual year  x1  x2  x3 
1   a 2001 5.980164 22.13975 45.08367 
2   a 2002 11.644311 34.67157 54.06608 
3   a 2003 11.805382 34.76187 63.64758 
4   a 2004 10.854982 28.44147 39.11835 
5   a 2005 10.586608 25.91022 39.29007 
6   b 2001 8.844076 18.37490 64.73601 

我现在有数据,比如说,在初始x1的格式x4并希望添加到x4 DATAS mydata等。我如何在R中做到这一点?

+0

首先'melt',然后使用'merge'功能添加到'mydata'。使用'by'参数来指定应该合并的标识符。或者单独合并X1和X4,然后熔化df。 – Jimbou

+0

检查'all','all.x',...参数。 – Jimbou

+0

'?xxx'应该始终是第一步;) – Jimbou

回答

1

下面是使用rvestpurrrdplyr一个解决方案:以同样的方式为X1

library(xml2) 
library(rvest) 
library(purrr) 
library(stringr) 
library(dplyr) 
URL <- "http://archive.thedailystar.net/2003/06/01/" 
page <- read_html(URL) 
# This is a CSS selector which pulls out all of the relevant `<td>` tags on the page 
links <- html_nodes(page, "table table table table table:not([width]) tr td:last-of-type") 
# Now retrieve all of the text within each td 
link_all_text <- map_chr(links, html_text) 
# Pull out those matching accident 
links_accident <- links[str_detect(link_all_text, "accident")] 
# Create a data frame with the links and both bits of text 
links_accident_detail <- map_df(links_accident, function(link) { 
    data_frame(href = link %>% html_node("a") %>% html_attr("href"), 
      link_text = link %>% html_node("a") %>% html_text, 
      next_line = link %>% html_node(".gistinhead") %>% html_text 
      ) 
}) 
links_accident_detail %>% as.data.frame() 
#    href      link_text 
#1 d30601100757.htm 2 killed in city road mishaps 
#                    next_line 
#1 Two unidentified men died in separate road accidents at Uttara and Tejgaon yesterday. 
+0

'links < - html_nodes(page,“table table table table:not([width])tr td:last-of-type”)来解决这个问题'''返回错误'no 'xml_find_all'的适用方法应用于“character”类的对象' – rsl

+0

抱歉遗漏了'read_html'语句。 –