Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to combine several cells into data frame with one cell is missing (NULL) in R

$
0
0

I want to combine several cells from multiple functions into one data frame. However, one function return with NULL result thus I can't combine all of them into one data frame. Anyway, I already have another existing data frame which will be updated (rbind) with this new data frame.

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


How can I adjust my code to combine them?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>