Re: [R] Filling empty List in a FOR LOOP
Try A[[1]] - NA (It is of course up to you to do the tests, presumably using if(), to decide when to assign NA to the list element.) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/31/12 7:53 PM, michaelyb cel81009...@gmail.com wrote: Hello all, I am trying to automate a list to be filled in a FOR LOOP. Basically I need to load info from a third source and store it into a list. This list goes to object A; The next list brought in by the LOOP goe to object B.. os on ans so forth. When the iteration on the LOOP goes well (which means it isn't empty) I get something like this: A [[1]] [,1] [1,] 1130 [2,] 1132 [3,] 1134 [4,] 1136 [5,] 1138 [6,] 1140 [7,] 1142 [[2]] [,1] [1,] 7.54 [2,] 6.55 [3,] 4.20 [4,] 3.50 [5,] 2.92 [6,] 2.42 [7,] 1.99 However when the source is empty I get the following: A [[1]] [,1] [[2]] [,1] I need to store NA in the latter one. I have tried A[is.na(A)] but I get an error message. Any help would be greatly appreciated. Thank you in advance -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4 522694.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filling empty List in a FOR LOOP
Ok. I appreciate your help. i don't understand what you guys mean by reproducible example, if you explained better, I will be more than glad to post it. Regards, -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4524532.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filling empty List in a FOR LOOP
Hello, michaelyb wrote Ok. I appreciate your help. i don't understand what you guys mean by reproducible example, if you explained better, I will be more than glad to post it. Regards, Try this: R A - list(a=NULL, b=NULL) R A R dput(A) Now, the output of 'dput' is a reproducible data example, just copy/paste/assign with '-' R X - structure(list(a = NULL, b = NULL), .Names = c(a, b)) And test the suggestions above. (Just the first.) R lapply(X, function(ll) ifelse(is.null(ll),NA,ll)) # works Use 'dput' and copypaste it's output here, in your post, for us to have an exact example of your data. Clear enough? Rui Barradas. -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4524695.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Filling empty List in a FOR LOOP
Hello all, I am trying to automate a list to be filled in a FOR LOOP. Basically I need to load info from a third source and store it into a list. This list goes to object A; The next list brought in by the LOOP goe to object B.. os on ans so forth. When the iteration on the LOOP goes well (which means it isn't empty) I get something like this: A [[1]] [,1] [1,] 1130 [2,] 1132 [3,] 1134 [4,] 1136 [5,] 1138 [6,] 1140 [7,] 1142 [[2]] [,1] [1,] 7.54 [2,] 6.55 [3,] 4.20 [4,] 3.50 [5,] 2.92 [6,] 2.42 [7,] 1.99 However when the source is empty I get the following: A [[1]] [,1] [[2]] [,1] I need to store NA in the latter one. I have tried A[is.na(A)] but I get an error message. Any help would be greatly appreciated. Thank you in advance -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4522694.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filling empty List in a FOR LOOP
Basically I need to read the data from an external source using many R commands. The problem is that sometimes the source is empty, and I get a list with empty values, and I need to substitute them by NA. It is def. not hard, I just don't see how In the prvious post, the first example had values on the source, but the second example didn't -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4523731.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filling empty List in a FOR LOOP
On Sun, Apr 1, 2012 at 9:59 AM, michaelyb cel81009...@gmail.com wrote: Basically I need to read the data from an external source using many R commands. The problem is that sometimes the source is empty, and I get a list with empty values, and I need to substitute them by NA. It is def. not hard, I just don't see how In the prvious post, the first example had values on the source, but the second example didn't And neither was reproducible as is this post. There is more than one way to get an empty list. I find R-list helpers are good but lacking in extrasensory perception skills. Frustrating, but if you insist on not following the posting guide, then you'll just have to figure it out for yourself. lapply(vector('list',2),function(ll) ifelse(is.null(ll),NA,ll)) # works lapply(list(,foo),function(ll) ifelse(is.null(ll),NA,ll)) # fail lapply(list(,foo),function(ll) ifelse(nchar(ll)1,NA,ll)) # works # ... # from your example 1 seems like you're expecting a matrix, so maybe you need nrow(ll) # etc. HTH -- View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4523731.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.