[R] indexing lists
Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]] __ 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] indexing lists
Chris, Well, the 'answer' could be: which(sapply(a, function(x) all(x == c(1,2 But I wonder how these elements of 'a' in your actual application are coming to be? If you're constructing them, you can give the elements of the list names, and then it doesn't matter what numerical index they have, you can just reference them by name. a - list(name1 = 1:2, name2 = 3:4) a a - c(anothername = list(9:10), a) a a$name1 Chris Carleton wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]] __ 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] indexing lists
Hi Chris, Does this do what you're after? It just compares each element of a (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are identical or not. which(sapply(a, identical, y = c(1, 2))) There were too many 1s floating around for me to figure out if you wanted to find elements of a that matched the entire vector or subelements of a that matched elements of the vector (if that makes any sense). HTH, Josh On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton w_chris_carle...@hotmail.com wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]] __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.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] indexing lists
On Nov 15, 2010, at 4:24 PM, Chris Carleton wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) It's a bit more involved than that (since which is expecting a vector of logicals and mapply is expecting a set of list arguments.) I needed to send mapply a MoreArgs list that would remain constant from test to test when using identical. which(mapply(identical, a, MoreArgs=list(c(1,2 [1] 1 (Admittedly very similar to Iverson's solution, but it is more readable to my eyes. On the other hand my method may stumble on more complex object with attributes. You should read hte identical help page at any rate.) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. But neither was testing the overall equality of a[1]'s contents with c(1,2) which appears to be your goal. Iverson's all and my identical accomplish that goal. Any thoughts? C -- David Winsemius, MD West Hartford, CT __ 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] indexing lists
Thanks for the suggestions. The issue for me is that the top level index is also like a database key so it might be a bit annoying to coerce it to char() so that I can reference it with a $ and then I would have to still be able to find out what the name was automatically. I've got a function right now that iterates through a list of values (db keys called cat values in this case) that returns an object from another function that will be used in yet another function. So, I have to store the objects and then pass each one in turn to a function while keeping track of what cat value that object is associated with so that it can be stored in relation to the cat value in a dataframe. Essentially like this... for i in cat { object - somefunction() alist[[ i ]] - object } for i in cat { result - somefunction(object[[ i ]]) adataframe[[ i, c('cat','result') ]] - c( i, result) } I'm paranoid about loosing track of which cat value is associated with which result and that's why I'm looking for a way to ensure that the output is stored correctly. The whole thing is going to be automated. Any suggestions would definitely be appreciated. I've tried just creating a list of lists to keep track of things so that list[[1]][[1]] is the cat value and list[[1]][[2]] is the associated object, but now I'm having trouble passing the object to the next function. This might take some time for me to work out. Thanks, Chris On 15 November 2010 16:38, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Chris, Does this do what you're after? It just compares each element of a (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are identical or not. which(sapply(a, identical, y = c(1, 2))) There were too many 1s floating around for me to figure out if you wanted to find elements of a that matched the entire vector or subelements of a that matched elements of the vector (if that makes any sense). HTH, Josh On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton w_chris_carle...@hotmail.com wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]] __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ [[alternative HTML version deleted]] __ 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] indexing lists
On Nov 15, 2010, at 5:07 PM, Chris Carleton wrote: Thanks for the suggestions. The issue for me is that the top level index is also like a database key so it might be a bit annoying to coerce it to char() so that I can reference it with a $ and then I would have to still be able to find out what the name was automatically. I've got a function right now that iterates through a list of values (db keys called cat values in this case) that returns an object from another function that will be used in yet another function. So, I have to store the objects and then pass each one in turn to a function while keeping track of what cat value that object is associated with so that it can be stored in relation to the cat value in a dataframe. Essentially like this... for i in cat { Please do not use cat as an object name for the same reason as not to use c or data as object names. object - somefunction() alist[[ i ]] - object } for i in cat { result - somefunction(object[[ i ]]) adataframe[[ i, c('cat','result') ]] - c( i, result) I don't think the [[ operator take two arguments. Perhaps you meant to use the [ operator. Even then cannot tell what cat is supposed to be and quoteing cat would prevent it from being evaluated. } I'm paranoid about loosing track of which cat value is associated with which result and that's why I'm looking for a way to ensure that the output is stored correctly. The whole thing is going to be automated. Any suggestions would definitely be appreciated. I've tried just creating a list of lists to keep track of things so that list[[1]][[1]] is the cat value and list[[1]][[2]] is the associated object, but now I'm having trouble passing the object to the next function. This might take some time for me to work out. It appears you are too busy to make a working example, so I am too busy to do it for you. ?names # for naming and access to names of list elements -- David. Thanks, Chris On 15 November 2010 16:38, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Chris, Does this do what you're after? It just compares each element of a (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are identical or not. which(sapply(a, identical, y = c(1, 2))) There were too many 1s floating around for me to figure out if you wanted to find elements of a that matched the entire vector or subelements of a that matched elements of the vector (if that makes any sense). HTH, Josh On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton w_chris_carle...@hotmail.com wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]] __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ [[alternative HTML version deleted]] __ 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. David Winsemius, MD West Hartford, CT __ 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] indexing lists
Thanks for the suggestions, but 'cat' is not causing name space conflicts for me and since I'm not packaging the code for anyone else to use, I'm less than concerned about potential conflicts. I did type that too quickly, and I have resolved my problem using a workaround that does not involve finding the names of top level list objects based on comparisons, but to clarify for anyone who is interested; adataframe[ i, c('cat','result') ] - c( i, result) allows me to assign the variable 'i' to the df column 'cat' and 'result' to the df column 'result' simultaneously for the ith entry (of course where 'i' is the row number). It just happens to be the case that 'i' is both the db key and the row name in the df, but since other portions of my code are shuffling the list I'm using to eventually fill the df it makes more sense for me to keep track of everything with the db key ('cat') than to try and use list indexes to keep track of where data is going. The solution I'm using is like this; for (i in npu_pdf_bw) { prob - fitted(npudist(bw=i[[2]],bwmethod=normal-reference,edat=origin[cols_select])) print(prob) pdf_pred[i[[1]],c('cat','probability')] - c(i[[1]],prob) } where npu_pdf_bw is a list of lists that each contains the 'cat' db key and an associated 'np' bandwidth object. 'pdf_pred' is a dataframe that holds the values and allows me to search for the result ('prob') on the basis of the 'cat' column thus maintaining my database integrity. It's perfectly acceptable for you to choose not to offer support on a volunteer help mailing list and I'm certain that support offered, when bitter or scathing, isn't appreciated in any event. C On 15 November 2010 17:59, David Winsemius dwinsem...@comcast.net wrote: On Nov 15, 2010, at 5:07 PM, Chris Carleton wrote: Thanks for the suggestions. The issue for me is that the top level index is also like a database key so it might be a bit annoying to coerce it to char() so that I can reference it with a $ and then I would have to still be able to find out what the name was automatically. I've got a function right now that iterates through a list of values (db keys called cat values in this case) that returns an object from another function that will be used in yet another function. So, I have to store the objects and then pass each one in turn to a function while keeping track of what cat value that object is associated with so that it can be stored in relation to the cat value in a dataframe. Essentially like this... for i in cat { Please do not use cat as an object name for the same reason as not to use c or data as object names. object - somefunction() alist[[ i ]] - object } for i in cat { result - somefunction(object[[ i ]]) adataframe[[ i, c('cat','result') ]] - c( i, result) I don't think the [[ operator take two arguments. Perhaps you meant to use the [ operator. Even then cannot tell what cat is supposed to be and quoteing cat would prevent it from being evaluated. } I'm paranoid about loosing track of which cat value is associated with which result and that's why I'm looking for a way to ensure that the output is stored correctly. The whole thing is going to be automated. Any suggestions would definitely be appreciated. I've tried just creating a list of lists to keep track of things so that list[[1]][[1]] is the cat value and list[[1]][[2]] is the associated object, but now I'm having trouble passing the object to the next function. This might take some time for me to work out. It appears you are too busy to make a working example, so I am too busy to do it for you. ?names # for naming and access to names of list elements -- David. Thanks, Chris On 15 November 2010 16:38, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Chris, Does this do what you're after? It just compares each element of a (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are identical or not. which(sapply(a, identical, y = c(1, 2))) There were too many 1s floating around for me to figure out if you wanted to find elements of a that matched the entire vector or subelements of a that matched elements of the vector (if that makes any sense). HTH, Josh On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton w_chris_carle...@hotmail.com wrote: Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a - list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they
Re: [R] indexing lists
On Nov 15, 2010, at 7:58 PM, Chris Carleton wrote: Thanks for the suggestions, but 'cat' is not causing name space conflicts for me install.packages( library(fortunes) fortune(dog) and since I'm not packaging the code for anyone else to use, I'm less than concerned about potential conflicts. We are concerned about conflicts in our brains. The hardware and wetware both need to be considered. snipped Presumably you next comment refers to: dw It appears you are too busy to make a working example, so I am too busy to do it for you. . It's perfectly acceptable for you to choose not to offer support on a volunteer help mailing list and I'm certain that support offered, when bitter or scathing, isn't appreciated in any event. Please ... now ... read the Posting Guide: http://www.R-project.org/posting-guide.html ... and learn to post in plain text. -- David. __ 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] indexing lists, using brobdingnagian
Dear R-Gurus, I have ended up with a calculation problem where I need to use brobs. I have to work my way through a vector with a for loop to act on each element in a calculation (refering to the previous value in the new vector of results -- so as far as I know I can't use apply) -- this produces a list of brobs. My problem is, how do I act on, plot this list, or do vector calculations with the elements? From the other emails I have read there does not seem to be a means of indexing a range of elements from a list. library(Brobdingnag) t-c(as.brob(5), as.brob(6), as.brob(7), as.brob(8), as.brob(10)) t [[1]] [1] +exp(1.6094) [[2]] [1] +exp(1.7918) [[3]] [1] +exp(1.9459) [[4]] [1] +exp(2.0794) [[5]] [1] +exp(2.3026) t[2:4] [[1]] [1] +exp(1.7918) [[2]] [1] +exp(1.9459) [[3]] [1] +exp(2.0794) So if you try plotting that against another variable: xval-c(1,2,3,4,5) plot(t,xval) you get nothing useful. Does anyone have some suggestions as to how to make this list of brobs more useful? I thought cbind might help (cbind(t)) but that did not work. Kind regards, Matt Redding DISCLAIMER**...{{dropped:15}} __ 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] indexing lists, using brobdingnagian
Hi all, Sorry about this, my brain finally ticked over. I just need to convert the list of brobs to a numeric vector (which I can do,as the answer to the calculation need not be a brob -- just the terms along the way). so nums-sapply(t,as.numeric) Does the trick in my case, I think. Matt Redding -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Redding, Matthew Sent: Wednesday, 28 May 2008 10:18 AM To: r-help@r-project.org Subject: [R] indexing lists, using brobdingnagian Dear R-Gurus, I have ended up with a calculation problem where I need to use brobs. I have to work my way through a vector with a for loop to act on each element in a calculation (refering to the previous value in the new vector of results -- so as far as I know I can't use apply) -- this produces a list of brobs. My problem is, how do I act on, plot this list, or do vector calculations with the elements? From the other emails I have read there does not seem to be a means of indexing a range of elements from a list. library(Brobdingnag) t-c(as.brob(5), as.brob(6), as.brob(7), as.brob(8), as.brob(10)) t [[1]] [1] +exp(1.6094) [[2]] [1] +exp(1.7918) [[3]] [1] +exp(1.9459) [[4]] [1] +exp(2.0794) [[5]] [1] +exp(2.3026) t[2:4] [[1]] [1] +exp(1.7918) [[2]] [1] +exp(1.9459) [[3]] [1] +exp(2.0794) So if you try plotting that against another variable: xval-c(1,2,3,4,5) plot(t,xval) you get nothing useful. Does anyone have some suggestions as to how to make this list of brobs more useful? I thought cbind might help (cbind(t)) but that did not work. Kind regards, Matt Redding DISCLAIMER**...{{dropped:15} } __ 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. DISCLAIMER The information contained in the above e-mail message or messages (which includes any attachments) is confidential and may be legally privileged. It is intended only for the use of the person or entity to which it is addressed. If you are not the addressee any form of disclosure, copying, modification, distribution or any action taken or omitted in reliance on the information is unauthorised. Opinions contained in the message(s) do not necessarily reflect the opinions of the Queensland Government and its authorities. If you received this communication in error, please notify the sender immediately and delete it from your computer system network. __ 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.