如何将多个单元格合并到数据帧中,但R中缺少一个单元格(NULL)
我想将多个函数中的几个单元格合并到一个数据帧中。 但是,一个函数返回的结果为如何将多个单元格合并到数据帧中,但R中缺少一个单元格(NULL),r,dataframe,web-scraping,rvest,R,Dataframe,Web Scraping,Rvest,我想将多个函数中的几个单元格合并到一个数据帧中。 但是,一个函数返回的结果为NULL,因此我无法将所有函数组合到一个数据帧中。无论如何,我已经有了另一个现有的数据帧,它将用这个新的数据帧更新(rbind) library(rvest) url <- "https://webscraper.io/test-sites/e-commerce/allinone/product/233" doc <- read_html(url) web <- function(node) {
NULL
,因此我无法将所有函数组合到一个数据帧中。无论如何,我已经有了另一个现有的数据帧,它将用这个新的数据帧更新(rbind
)
library(rvest)
url <- "https://webscraper.io/test-sites/e-commerce/allinone/product/233"
doc <- read_html(url)
web <- function(node) {
doc %>%
html_nodes(node) %>%
html_text() %>%
gsub("\n", "", .) %>%
trimws()}
web_na <- possibly(web, otherwise = 0)
web1 <- web_na("h1")
web2 <- web_na(".price")
web3 <- web_na(".just-random-nodes")
df_web <- data.frame(web1, web2, web3)
==========
Result:
Error in data.frame(web1, web2, web3) : arguments imply differing number of rows: 1, 0
Expected Result:
web1 web2 web3
<fctr> <fctr> <fctr>
Test Sites $520.99 0 or NA
库(rvest)
url%
gsub(“\n”,”,)%>%
trimws()}
web\u na为web3添加此功能非常有效。您可能需要为web1和web2添加类似的语句:
web3 <- ifelse(length(web3)==0, NA, web3)
df_web <- data.frame(web1, web2, web3)
df_web
# web1 web2 web3
# 1 Test Sites $520.99 NA
web3我能想到的最简单的解决方案是类似于df\u web的东西