Error In If Argument Is Of Length Zero
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring argument is of length zero shiny developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Error In 1:object$nsdf : Argument Of Length 0
Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join r logical(0) them; it only takes a minute: Sign up argument is of length zero up vote 11 down vote favorite I am very new to R and am trying to learn scraping. I am trying to use the following syntax
R Integer(0)
to get the occupation information from George Clooney's wikipedia page. Eventually I would like there to be a loop to get data on various personalitys' occupations. However, I get the following problem running the below code: Error in if (symbol != "role") symbol = NULL : argument is of length zero I am not sure why this keeps on coming up. Any assistance will be appreciated. library(XML) library(plyr) url = 'http://en.wikipedia.org/wiki/George_Clooney' # don't forget to parse the HTML, doh! numeric(0) in r doc = htmlParse(url) # get every link in a table cell: links = getNodeSet(doc, '//table/tr/td') # make a data.frame for each node with non-blank text, link, and 'title' attribute: df = ldply(links, function(x) { text = xmlValue(x) if (text=='') text=NULL symbol = xmlGetAttr(x, 'class') if (symbol!='role') symbol=NULL if(!is.null(text) & !is.null(symbol)) data.frame(symbol, text) } ) Thank you Amar r share|improve this question edited Jul 2 '12 at 17:13 Brian Diggs 33.2k781124 asked Jul 2 '12 at 14:25 user1496289 4532611 2 Debugging advice: stackoverflow.com/a/5156351/636656 . Specifically, try options(error=recover) here. –Ari B. Friedman Jul 2 '12 at 14:34 the problem is most likely that symbol is NULL. See what happens with if(NULL != "role") print('test'). Something like this should work, although I didn't run your code: if (!is.null(symbol) && symbol != 'role') symbol <- NULL –GSee Jul 2 '12 at 14:41 add a comment| 1 Answer 1 active oldest votes up vote 21 down vote As @gsee mentioned, you need to check that symbol isn't NULL before you check its value. Here's a minor update to your code that works (at least for George). df = ldply( links, function(x) { text = xmlValue(x) if (!nzchar(text)) text = NULL symbol = xmlGetAttr(x, 'class') if (!is.null(symbol) && symbol != 'role') symbol = NULL if(!is.null(text) & !is.null(symbol)) data.frame(symbol, text) } ) share|improve this answer edited Oct 26 '14 at 5:06 answered Jul 2 '12 at 17:44 Richie C
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more
R Character(0)
about Stack Overflow the company Business Learn more about hiring developers or posting ads
Is.null R
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow error in if missing value where true/false needed is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error in if/while (condition) { : argument is of length zero up http://stackoverflow.com/questions/11295618/argument-is-of-length-zero vote 20 down vote favorite 3 I received the error Error in if (condition) { : argument is of length zero or Error in while (condition) { : argument is of length zero What causes this error message, and what does it mean? On further inspection it seems that the value is NULL. condition ## NULL In order to deal with this error, how do I test for NULL values? http://stackoverflow.com/questions/12307139/error-in-if-while-condition-argument-is-of-length-zero I expected that this would return TRUE, but I got an empty logical value: condition == NULL ## logical(0) r r-faq share|improve this question edited Dec 26 '14 at 16:23 Richie Cotton 57.3k16126231 asked Sep 6 '12 at 19:34 Craig Wright 9761616 use is.null instead of == –Matthew Plourde Sep 6 '12 at 19:37 add a comment| 3 Answers 3 active oldest votes up vote 29 down vote accepted See ?NULL You have to use is.null ‘is.null’ returns ‘TRUE’ if its argument is ‘NULL’ and ‘FALSE’ otherwise. Try this: if ( is.null(hic.data[[z]]) ) { print("is null")} From section 2.1.6 of the R Language Definition There is a special object called NULL. It is used whenever there is a need to indicate or specify that an object is absent. It should not be confused with a vector or list of zero length. The NULL object has no type and no modifiable properties. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL. share|improve this answer edited Sep 6 '12 at 19:49 answered Sep 6 '12 at 19:37 GSee 29.2k76994 add a comment| up vote 4 down vote What causes
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn http://stackoverflow.com/questions/29332457/r-argument-is-of-length-zero-in-ifcondition more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags http://stats.stackexchange.com/questions/141404/arguments-of-length-zero-error-with-very-similar-code-while-forecasting Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up R “argument is of length zero” in if(condition) up vote 1 down vote favorite Very new to R, would appreciate if you could tell me error in what I’m doing wrong. This was my initial code: count <- 0 for (i in 1:nrow(data)) { if(outcome[i]==data[1,i]) inc(count) <- 1 #package Hmisc } where outcome and data[1] are vectors with binary values eg. [1, 0, 0, 1, 1, 0..] I got the result ‘argument is of length zero’ with the if statement. Tested it individually with rows that matched/mismatched, discovered that the result shows up only in the latter. So I amended my code to: error in if count <- 0 for (i in 1:nrow(data)) { if(!is.null(outcome[i]==data[1,i])) inc(count) <- 1 #package Hmisc } Now the value that count returned is the number of ‘for' iterations. Doesn’t this mean R is NOT returning a null value for ANY mismatch in the new code?? How is this happening? Solutions please? r if-statement arguments zero share|improve this question asked Mar 29 '15 at 17:36 sheffy17 84 1 You're looping through the number of rows in data, but comparing with the values of the first row for different columns. If it's indeed 2-dimensional and you want the first column, try data[i, 1]. –Molx Mar 29 '15 at 17:53 Okay, that was incredibly careless of me; and that does also fix the issue! thanks. –sheffy17 Mar 29 '15 at 20:39 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted If data is a one dimension array (the binary data you showed is) rather then a matrix, vector, or other structure you can use length to find the row count. This code shows how to add a count based on matches with a one dimensional array (note I assume inc is a pascal like custom function). data = c(1, 0, 0, 1, 1, 0) outcome = c(0, 0, 1, 0, 1, 0) count <- 0 for (i in 1:length(data)) {
Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Cross Validated Questions Tags Users Badges Unanswered Ask Question _ Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top arguments of length zero error with very similar code while forecasting up vote 0 down vote favorite When I forecast from a linear Regression model in R using the following code, I get an arguments of length zero error , which I understand as a null pointer: library(forecast) Mwh = c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0) temp = c(29.3,21.7,23.7,10.4,29.7,11.9,9.0,23.4,17.8,30.0,8.6,11.8) t=data.frame(Mwh,temp) fit = lm(Mwh~temp,data=t) fcast=forecast(fit,newdata=35) However, the following code, which looks very, very similar to me, does not produce the error, but the forecast as desired - why? Where is my blind spot? This works: library(forecast) electricity =c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0) maximumDailyTemp= c(29.3,21.7,23.7,10.4,29.7,11.9,9.0,23.4,17.8,30.0,8.6,11.8) d = data.frame(maximumDailyTemp, electricity) fit<-lm(electricity ~ maximumDailyTemp , data=d) electricityForecast<-forecast(fit,newdata=35) Why is there an error above and no error below? The order of variables in the data Frames cannot be the reason, right? r forecasting share|improve this question edited Mar 12 '15 at 8:24 asked Mar 12 '15 at 8:17 Statos 788 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted This problem seems to happen because of using $t$ for the dataframe. However, in R, $t$ is a command for the transpose operation. The below code (using $d$) works! library(forecast) Mwh = c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0) temp = c(29.3,2