-2

Resetting the length of an object using if statement

I am doing web scraping. There is an if statement as follows

{if(length(title) == 0) NA else title}

Originally length of title is 12.

Now here I am confused over length function.

length(title) gives me 12. length(title)==0 gives me FALSE. So, putting length(title)==0 makes no sense because we already know that length of title is 12, then what's the point of putting it to 0.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 4
    Please add some sample data to your question. It is difficult to figure out where you problem is here. – Tim Biegeleisen Jun 10 '19 at 09:46
  • 3
    What exactly is your question? You need the logical expression in order for the `if` to know whether to evaluate or not. Are you confusing `length` with `nchar`? – Sotos Jun 10 '19 at 09:56

1 Answers1

0

Maybe it is because when there is no title would be a problem with the subsequent code. For example it there is no title print will not have the expected behavior.

> title1=c()
> length(title1)
[1] 0
> print(title1)
NULL
> print(NA)
[1] NA
Robert
  • 5,038
  • 1
  • 25
  • 43
  • https://stackoverflow.com/questions/45901532/inputting-na-where-there-are-missing-values-when-scraping-with-rvest . Here is the link to my problem, in the solution if statement is used where i am not able to figure out how length function equates every element to 0 while when i compute length of title or length(one of the names in list) using the if function the result is the character names that can be seen in the table in the link. – deepanshu Jun 10 '19 at 11:53