Suppose I have the following:
test <- list(a=1,b=2,c=3) I also have a vector (or list, or something else...) with new numbers new <- c(4,5,6) What I'm trying to figure out is how to take the list, and update the numbers from {1,2,3} to {4,5,6} So, in the end,I want the 'update' test list to look like (a=4,a=5,a=6) I tried a bunch of obvious things I know about 'replacing' things (without success), but the problem in this instance seems to be the fact that the list contains elements that are expressions (a=1, a=2,...), while the new vector is simply a set of numbers. So, I want to change the numbers in the list, but retain the character parts of the expressions in the list (I need to have the list of expressions as is for other purposes). Doable? Thanks in advance... ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Solved it:
test <- list(a=1,b=2,c=3) new <- c(4,5,6) hold <- as.list(new) updated_test <- replace(test,c(1:3),hold) $a [1] 4 $b [1] 5 $c [1] 6 mean.parms <- as.list(mean.parms) mm.parms <- replace(far.parms,c(1:length(far.parms)),mean.parms) On 9/22/2017 10:34 AM, Evan Cooch wrote: > Suppose I have the following: > > test <- list(a=1,b=2,c=3) > > I also have a vector (or list, or something else...) with new numbers > > new <- c(4,5,6) > > What I'm trying to figure out is how to take the list, and update the > numbers from {1,2,3} to {4,5,6} > > So, in the end,I want the 'update' test list to look like > > (a=4,a=5,a=6) > > I tried a bunch of obvious things I know about 'replacing' things > (without success), but the problem in this instance seems to be the > fact that the list contains elements that are expressions (a=1, > a=2,...), while the new vector is simply a set of numbers. > > So, I want to change the numbers in the list, but retain the character > parts of the expressions in the list (I need to have the list of > expressions as is for other purposes). > > Doable? > > Thanks in advance... > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Well, that's a bit like driving from Boston to New York by way of Chicago.
See ?structure test <- list(a=1,b=2,c=3) new <- c(4,5,6) test.new <- structure(as.list(new), names=names(test)) test.new $a [1] 4 $b [1] 5 $c [1] 6 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Sep 22, 2017 at 7:51 AM, Evan Cooch <[hidden email]> wrote: > Solved it: > > test <- list(a=1,b=2,c=3) > new <- c(4,5,6) > > hold <- as.list(new) > updated_test <- replace(test,c(1:3),hold) > > $a > [1] 4 > > $b > [1] 5 > > $c > [1] 6 > > > > mean.parms <- as.list(mean.parms) > > mm.parms <- replace(far.parms,c(1:length(far.parms)),mean.parms) > > > On 9/22/2017 10:34 AM, Evan Cooch wrote: > >> Suppose I have the following: >> >> test <- list(a=1,b=2,c=3) >> >> I also have a vector (or list, or something else...) with new numbers >> >> new <- c(4,5,6) >> >> What I'm trying to figure out is how to take the list, and update the >> numbers from {1,2,3} to {4,5,6} >> >> So, in the end,I want the 'update' test list to look like >> >> (a=4,a=5,a=6) >> >> I tried a bunch of obvious things I know about 'replacing' things >> (without success), but the problem in this instance seems to be the fact >> that the list contains elements that are expressions (a=1, a=2,...), while >> the new vector is simply a set of numbers. >> >> So, I want to change the numbers in the list, but retain the character >> parts of the expressions in the list (I need to have the list of >> expressions as is for other purposes). >> >> Doable? >> >> Thanks in advance... >> >> >> > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Thanks!
On 9/22/2017 12:34 PM, Bert Gunter wrote: > Well, that's a bit like driving from Boston to New York by way of > Chicago. > > See ?structure > > test <- list(a=1,b=2,c=3) > new <- c(4,5,6) > test.new <- structure(as.list(new), names=names(test)) > test.new > $a > [1] 4 > > $b > [1] 5 > > $c > [1] 6 > > Cheers, > Bert > > > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Fri, Sep 22, 2017 at 7:51 AM, Evan Cooch <[hidden email] > <mailto:[hidden email]>> wrote: > > Solved it: > > test <- list(a=1,b=2,c=3) > new <- c(4,5,6) > > hold <- as.list(new) > updated_test <- replace(test,c(1:3),hold) > > $a > [1] 4 > > $b > [1] 5 > > $c > [1] 6 > > > > mean.parms <- as.list(mean.parms) > > mm.parms <- replace(far.parms,c(1:length(far.parms)),mean.parms) > > > On 9/22/2017 10:34 AM, Evan Cooch wrote: > > Suppose I have the following: > > test <- list(a=1,b=2,c=3) > > I also have a vector (or list, or something else...) with new > numbers > > new <- c(4,5,6) > > What I'm trying to figure out is how to take the list, and > update the numbers from {1,2,3} to {4,5,6} > > So, in the end,I want the 'update' test list to look like > > (a=4,a=5,a=6) > > I tried a bunch of obvious things I know about 'replacing' > things (without success), but the problem in this instance > seems to be the fact that the list contains elements that are > expressions (a=1, a=2,...), while the new vector is simply a > set of numbers. > > So, I want to change the numbers in the list, but retain the > character parts of the expressions in the list (I need to have > the list of expressions as is for other purposes). > > Doable? > > Thanks in advance... > > > > ______________________________________________ > [hidden email] <mailto:[hidden email]> mailing list -- > To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Free forum by Nabble | Edit this page |