[R-lang] Re: swapping values without temporary storage

Michael Shvartsman mshvarts@umich.edu
Tue Apr 15 14:53:51 PDT 2014


D'oh, you're right. Thanks, Ewan -- and sorry, everyone else. Agreed 
that Martin's version is cleanest.
> Ewan Dunbar <mailto:ewan.dunbar@alumni.utoronto.ca>
> April 15, 2014 4:27 PM
> to be clear, swap() from Kmisc specifies the swappees not by index but 
> by contents:
>
> library(Kmisc)
> x <- 11:20
> x <- swap(x, c(2,4), c(4,2))
> # not the desired answer - nothing happened because 2 and 4 weren't in 
> the vector
> [1] 11 12 13 14 15 16 17 18 19 20
> x <- swap(x, c(12,14), c(14,12))
> # probably wanted this
> [1] 11 14 13 12 15 16 17 18 19 20
>
> fwiw, I personally would use mc's version (x[c(2,4)] <- x[c(4,2)]) but 
> what's most clear depends on your audience, which includes but is 
> never limited to you. wrapping in a function with a meaningful name 
> does not hurt. as long as you let me take back what I suggested before 
> :) - it has clunkiness of exactly the kind that indexing on the left 
> hand side is there to avoid. -e
>
>
>
>
> Michael Shvartsman <mailto:mshvarts@umich.edu>
> April 15, 2014 2:45 PM
> Are you looking for a clever one-liner for clarity or are you looking 
> to avoid copying for memory and performance reasons? In the former 
> case, swap() in Kmisc provides what you want.
>
> library(Kmisc)
>
> x <- 1:10
> x
> x <- swap(x, c(2,3), c(3,2))
> x
> x <- swap(x, c(2,10), c(10,2))
> x
>
> In the latter case, your best bet is to compile R with 
> --enable-memory-profiling (OSX R does this by default) and use 
> tracemem() and Rprofmem().
>
>     Hope this helps!
>
>         Mike.
>
>
>
> João Veríssimo <mailto:jl.verissimo@gmail.com>
> April 15, 2014 2:05 PM
> Perhaps rev() can help?
>
> Maybe something like:
>
> x <- c("a", "b", "c")
> c(x[1], rev(x[2:3]))
>
> João
>
>
> Hofmeister, Philip <mailto:phofme@essex.ac.uk>
> April 15, 2014 1:52 PM
> this is probably more appropriate for a general R listserv, but i'm 
> more likely to get an answer i can decipher here. apologies also for 
> what is likely a simple fix, but i can't seem to find the right term 
> or syntax to get this to work without writing ugly code.
>
> i want to swap values in a vector, e.g.
>
> x <- c(1, 2, 3)
> [1] 1 2 3
>
> and get back
>
> 1 3 2
>
> i can do this by saving the 2nd or 3rd element of the vector 
> elsewhere, replacing one of the vector elements (x[3] <- x[2]), and 
> then restoring the stored value, but that seems wasteful. surely there 
> is a function like swap() or std::swap(a, b), but i can't seem to find 
> anything. anyone have any ideas?
>
> ph
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucsd.edu/pipermail/ling-r-lang-l/attachments/20140415/9dc91b15/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
Url : http://mailman.ucsd.edu/pipermail/ling-r-lang-l/attachments/20140415/9dc91b15/attachment.jpg 


More information about the ling-r-lang-L mailing list